View Single Post
Old Apr 26th, 2005, 1:33 AM   #15
zxasqwedc
Newbie
 
Join Date: Apr 2005
Posts: 5
Rep Power: 0 zxasqwedc is an unknown quantity at this point
thanks for your help....

here is my sollution clever guys...


public class SparseVector{

	protected int dimension=0;
	private int index=0;
	public NonZero[] vector;
	
	public SparseVector(int dim){
		dimension=dim;
		
	}
	
	
	public void put(int loc,int val){
		NonZero a=new NonZero(loc,val);
		vector[index]=a;
		index=index+1;
	}
	
	
	public int get(int where){
		int walue=0;
		for(int i=0;i<=index;i++){
			if(vector[i].location==where)
			walue=vector[i].value;
			
		}
		return walue;
	}
	
	
	
	public SparseVector add(SparseVector a,SparseVector b){
		
		int temp1=0; 
		int temp2=0;
		int control=2;
		SparseVector sum=new SparseVector(a.dimension);
		for(int y=0;y<(a.vector).length;y++){
			int locA=(a.vector)[y].location;
			for(int z=0;z<(b.vector).length;z++){
				if(locA==(((b.vector)[z]).location)){
					temp1=((a.vector)[y].value)+((b.vector)[z].value);
					control=1;
				}
				else{
					temp2=((a.vector)[y].value);
							
				}
				if(control==1)
				sum.put(locA,temp1);
				else
				sum.put(locA,temp2);
				
				control=2;	
			}
		}
		
		for(int x=0;x<(b.vector).length;x++){
			int locB=(b.vector)[x].location;
			for(int v=0;v<(a.vector).length;v++){
				if(locB==(a.vector)[v].location){
					control=1;
				}
				else{
					temp2=(b.vector)[x].value;
				}
				
				if(control==2)
					sum.put(locB,temp2);
					
				control=2;
			}
		}
		
		
		return sum;
		
		
	}
	
	
	
	
	public SparseVector multiply(int constant){
		for(int m=0;m<(this.vector).length;m++){
			(this.vector)[m].value=((this.vector)[m].value)*constant;
						
		}
	return this;	
	}
	
	

	
	
	
}



public class NonZero{
	public int location,locationX,locationY, value;
	
	public NonZero(int loc, int val){
		location=loc;
		value=val;
	}
	
	public NonZero(int locx,int locy, int val){
		locationX=locx;
		locationY=locy;
		value=val;
	}
}
zxasqwedc is offline   Reply With Quote