[marf-cvs] marf/src/marf/math Vector.java,NONE,1.1
Brought to you by:
mokhov
From: <mo...@us...> - 2003-05-04 20:32:48
|
Update of /cvsroot/marf/marf/src/marf/math In directory sc8-pr-cvs1:/tmp/cvs-serv18837 Added Files: Vector.java Log Message: Add initial version of marf.math.Vector. Like Matrix, was conversted from the same C++ code. So far remains unused (except test.java), but eventually we might need it. --- NEW FILE: Vector.java --- package marf.math; import marf.MARF; /** * <p>Algebraic operations on vectors. Adapted from my graphics project written in C++.</p> * <p>$Header: /cvsroot/marf/marf/src/marf/math/Vector.java,v 1.1 2003/05/04 20:32:45 mokhov Exp $</p> * <p><b>NOTE:</b> this class has the same issues as marf.math.Matrix</p> * * @author Serguei A. Mokhov, mo...@cs... * @since 0.3.0 * @see marf.math.Matrix */ public class Vector extends Matrix { public static final int DEFAULT_VECTOR_LENGTH = 3; // Object Live Cycle public Vector() { this(DEFAULT_VECTOR_LENGTH); } public Vector(final int piVectorLength) { super(piVectorLength, 1); } public Vector(final Vector p_oVector) { super(p_oVector); } public Vector(final Matrix p_oMatrix) { super(p_oMatrix); } public Vector(final double[] padVectorData) { super(padVectorData); } // Vector Specific Operations public final double getElement(final int p_iPosition) { return m_iRows == 1 ? super.getElement(0, p_iPosition) : super.getElement(p_iPosition, 0); } public void setElement(final int p_iPosition, final double p_dValue) { if(m_iRows == 1) { super.setElement(0, p_iPosition, p_dValue); } else { super.setElement(p_iPosition, 0, p_dValue); } } public double getLength() { double l_dSqSum = 0.0; for(int i = 0; i < getElements(); i++) { l_dSqSum += m_pdMatrix[i] * m_pdMatrix[i]; } return Math.sqrt(l_dSqSum); } public final Vector getUnitVector() { Vector l_oUnitVector = (Vector)this.clone(); double l_dThisLength = l_oUnitVector.getLength(); for(int i = 0; i < getElements(); i++) { l_oUnitVector.setElement(i, getElement(i) / l_dThisLength); } return l_oUnitVector; } public Vector getInnerProduct(final Vector p_oLHSVector, final Vector p_oRHSVector) { Vector l_oXVector = new Vector(); l_oXVector.setElement(0, (p_oLHSVector.getElement(1) * p_oRHSVector.getElement(2) - p_oLHSVector.getElement(2) * p_oRHSVector.getElement(1))); l_oXVector.setElement(1, (p_oLHSVector.getElement(2) * p_oRHSVector.getElement(0) - p_oLHSVector.getElement(0) * p_oRHSVector.getElement(2))); l_oXVector.setElement(2, (p_oLHSVector.getElement(0) * p_oRHSVector.getElement(1) - p_oLHSVector.getElement(1) * p_oRHSVector.getElement(0))); return l_oXVector; } public Vector getCrossProduct(final Vector p_oLHSVector, final Vector p_oRHSVector) { MARF.debug("Vector.getInnerProduct() - WARNING: Implementation is incomplete!\n"); if(p_oLHSVector.getElements() != p_oRHSVector.getElements()) { return new Vector(0); } Vector l_oVectorIP = new Vector(p_oLHSVector.getElements()); for(int i = 0; i < l_oVectorIP.getElements(); i++) { l_oVectorIP.setElement(i, p_oLHSVector.getElement(i) * p_oRHSVector.getElement(i)); } return l_oVectorIP; } public double getDotProduct(final Vector p_oLHSVector, final Vector p_oRHSVector) { double l_dDotProduct = 0.0; if(p_oLHSVector.getElements() != p_oRHSVector.getElements()) { MARF.debug("Vector.getDotProduct() - WARNING: Number of elements in vectors do not match: lhs="+p_oLHSVector.getElements()+", rhs="+p_oRHSVector.getElements()+"\n"); return -1.0; } for(int i = 0; i < p_oLHSVector.getElements(); i++) { l_dDotProduct += p_oLHSVector.getElement(i) * p_oRHSVector.getElement(i); } return l_dDotProduct; } public boolean isOrthogonal(final Vector p_oVector) { return (getDotProduct(this, p_oVector) == 0); } public void normalize() { double l_dLength = getLength(); // If fraction is too small // avoid division by zero // and just set all elements // to zero... if(Math.abs(l_dLength) < Double.MIN_VALUE) { setAll(); } // ... or devide by length otherwise else { for(int i = 0; i < m_iCols * m_iRows; i++) { m_pdMatrix[i] /= l_dLength; } } } public final Vector i() { return new Vector(new double[] {1, 0, 0}); } public final Vector j() { return new Vector(new double[] {0, 1, 0}); } public final Vector k() { return new Vector(new double[] {0, 0, 1}); } } // EOF |