use of org.hipparchus.linear.Array2DRowRealMatrix in project symja_android_library by axkr.
the class Roots method findRoots.
/**
* <p>
* Given a set of polynomial coefficients, compute the roots of the
* polynomial. Depending on the polynomial being considered the roots may
* contain complex number. When complex numbers are present they will come
* in pairs of complex conjugates.
* </p>
*
* @param coefficients
* coefficients of the polynomial.
* @return the roots of the polynomial
*/
@Nonnull
public static IAST findRoots(double... coefficients) {
int N = coefficients.length - 1;
// Construct the companion matrix
RealMatrix c = new Array2DRowRealMatrix(N, N);
double a = coefficients[N];
for (int i = 0; i < N; i++) {
c.setEntry(i, N - 1, -coefficients[i] / a);
}
for (int i = 1; i < N; i++) {
c.setEntry(i, i - 1, 1);
}
try {
IAST roots = F.List();
EigenDecomposition ed = new EigenDecomposition(c);
double[] realValues = ed.getRealEigenvalues();
double[] imagValues = ed.getImagEigenvalues();
for (int i = 0; i < N; i++) {
roots.append(F.chopExpr(F.complexNum(realValues[i], imagValues[i]), Config.DEFAULT_ROOTS_CHOP_DELTA));
}
return roots;
} catch (Exception ime) {
throw new WrappedException(ime);
}
}
use of org.hipparchus.linear.Array2DRowRealMatrix in project symja_android_library by axkr.
the class Convert method list2RealMatrix.
/**
* Returns a RealMatrix if possible.
*
* @param listMatrix
* @return a RealMatrix or <code>null</code> if the given list is no matrix.
* @throws WrongArgumentType
* if not all elements are convertable to a <code>double</code>
* value.
* @throws ClassCastException
* @throws IndexOutOfBoundsException
* @deprecated use {@link IExpr#toRealMatrix()}
*/
public static RealMatrix list2RealMatrix(final IAST listMatrix) throws ClassCastException, IndexOutOfBoundsException {
if (listMatrix == null) {
return null;
}
if (listMatrix instanceof ASTRealMatrix) {
return ((ASTRealMatrix) listMatrix).getRealMatrix();
}
final Object header = listMatrix.head();
if (header != F.List) {
return null;
}
IAST currInRow = (IAST) listMatrix.arg1();
if (currInRow.isAST0()) {
// special case 0-Matrix
double[][] array = new double[0][0];
return new Array2DRowRealMatrix(array, false);
}
final double[][] elements = Expr2Object.toDoubleMatrix(listMatrix);
return new Array2DRowRealMatrix(elements, false);
}
Aggregations