Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Apr 2017 |
Last Sign in: | 10 Weeks Ago, 4 Days Ago |
Questions Answered: | 4870 |
Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Write Java (Sparse Vector) for the following two ADD and SUBTRACT methods using DOUBLYLINKEDLIST
A)
Vector addition produces the sum of two vectors, the sum vector. If an element with a particular index exists in both vectors then add the values of the elements together to form the element with that index in the sum vector. If the sum of the two values is zero then that element does not appear in the sum vector. If an element with a particular index exists in only one vector then copy that element to the sum vector.
An example:
A is ([3, 1.0], [2500, 6.3], [5000, 10.0], [60000, 5.7]) B is ([1, 7.5], [3, 5.7], [2500, -6.3])
A + B is ([1, 7.5], [3, 6.7], [5000, 10.0], [60000, 5.7])
a.The add method: public SparseVector add(SparseVector sv), which performs the
addition of two sparse vectors. For example, if A and B are sparse vectors then A.add(B) returns a
SparseVector which is A+B.
B)
Vector subtraction produces the difference of two vectors, the difference vector. If an element with a particular index exists in both vectors then subtract the value of the element in the second vector from the value of the element in the first vector to form the element with that index in the difference vector. If the difference of the two values is zero then that element does not appear in the difference vector. If an element with a particular index exists in the first vector but not the second vector then copy that element to the difference vector. If an element with a particular index exists in the second vector but not the first vector then copy that element to the difference vector and negate its value.
An example:
A is ([3, 1.0], [2500, 6.3], [5000, 10.0], [60000, 5.7])
B is ([1, 7.5], [3, 5.7], [2500, -6.3])
A - B is ([1, -7.5], [3, -5.7], [2500, 12.6], [5000, 10.0], [60000, 5.7])
b. The subtract method: public SparseVector subtract(SparseVector sv), which
performs the subtraction of two sparse vectors. For example, if A and B are sparse vectors then
A.subtract(B) returns a SparseVector which is A-B.
Â