Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public SparseMatrix< Arithmetic > add( SparseMatrix< Arithmetic > s )
- {
- ArrayList< Term > list2 = s.getList();
- int ndx1 = 0;
- int ndx2 = 0;
- Term x1 = list.get(ndx1);
- Term x2 = list2.get(ndx2);
- SparseMatrix< Arithmetic > result = new SparseMatrix< Arithmetic >();
- while( ndx1 < list.size() && ndx2 < list2.size() )
- {
- x1 = list.get( ndx1 );
- x2 = list2.get( ndx2 );
- if( x1.compareTo(x2) > 0 )
- {
- result.insert(x2);
- ndx2++;
- }
- else if( x1.compareTo(x2) < 0 )
- {
- result.insert(x1);
- ndx1++;
- }
- else
- {
- Arithmetic value1 = x1.getValue();
- Arithmetic value2 = x2.getValue();
- Arithmetic total = value1.add(value2);
- result.insert(x1.getRow(),x1.getCol(),total);
- ndx1++;
- ndx2++;
- }
- }
- if( ndx1 < list.size() )
- {
- while( ndx1 < list.size() )
- {
- x1 = list.get(ndx1);
- result.insert(x1);
- ndx1++;
- }
- result.insert(x1);
- }
- if( ndx2 < list2.size() )
- {
- while( ndx2 < list2.size() )
- {
- x2 = list2.get(ndx2);
- result.insert(x2);
- ndx2++;
- }
- result.insert(x2);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement