Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public SparseMatrix< Arithmetic > add( SparseMatrix< Arithmetic > s )
  2. {
  3. ArrayList< Term > list2 = s.getList();
  4. int ndx1 = 0;
  5. int ndx2 = 0;
  6. Term x1 = list.get(ndx1);
  7. Term x2 = list2.get(ndx2);
  8. SparseMatrix< Arithmetic > result = new SparseMatrix< Arithmetic >();
  9.  
  10. while( ndx1 < list.size() && ndx2 < list2.size() )
  11. {
  12. x1 = list.get( ndx1 );
  13. x2 = list2.get( ndx2 );
  14.  
  15. if( x1.compareTo(x2) > 0 )
  16. {
  17. result.insert(x2);
  18. ndx2++;
  19. }
  20. else if( x1.compareTo(x2) < 0 )
  21. {
  22. result.insert(x1);
  23. ndx1++;
  24. }
  25. else
  26. {
  27. Arithmetic value1 = x1.getValue();
  28. Arithmetic value2 = x2.getValue();
  29. Arithmetic total = value1.add(value2);
  30. result.insert(x1.getRow(),x1.getCol(),total);
  31. ndx1++;
  32. ndx2++;
  33. }
  34. }
  35. if( ndx1 < list.size() )
  36. {
  37. while( ndx1 < list.size() )
  38. {
  39. x1 = list.get(ndx1);
  40. result.insert(x1);
  41. ndx1++;
  42. }
  43. result.insert(x1);
  44. }
  45. if( ndx2 < list2.size() )
  46. {
  47. while( ndx2 < list2.size() )
  48. {
  49. x2 = list2.get(ndx2);
  50. result.insert(x2);
  51. ndx2++;
  52. }
  53. result.insert(x2);
  54. }
  55.  
  56. return result;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement