Search code examples
javapythonarraysnormalizing

How to add/subtract two arrays in java


Anyone know how to convert those python program to java? i have no idea how to add/subtract two array and normalizing array in java, or i need using matrix?

P1 = array([xA, yA, zA])
P2 = array([xB, yB, zB])
P3 = array([xC, yC, zC])

ex = (P2 - P1)/(numpy.linalg.norm(P2 - P1))
i = dot(ex, P3 - P1)
ey = (P3 - P1 - i*ex)/(numpy.linalg.norm(P3 - P1 - i*ex))
ez = numpy.cross(ex,ey)
d = numpy.linalg.norm(P2 - P1)
j = dot(ey, P3 - P1)

triPt = P1 + x*ex + y*ey + z*ez

Solution

  • If you use plain arrays, you would need to loop through the elements. Consider using a Matrix package, e.g. http://math.nist.gov/javanumerics/jama/.

    Something like (assuming a is a 2d-array):

    import Jama.*
    
    Matrix A = new Matrix(a);
    Matrix B = new Matrix(b);
    Matrix R = A.minus(B);