|
| 1 | +package com.baeldung.suanshu; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.Matrix; |
| 9 | +import com.numericalmethod.suanshu.algebra.linear.vector.doubles.Vector; |
| 10 | +import com.numericalmethod.suanshu.algebra.linear.vector.doubles.dense.DenseVector; |
| 11 | +import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.matrixtype.dense.DenseMatrix; |
| 12 | +import com.numericalmethod.suanshu.algebra.linear.matrix.doubles.operation.Inverse; |
| 13 | +import com.numericalmethod.suanshu.analysis.function.polynomial.Polynomial; |
| 14 | +import com.numericalmethod.suanshu.analysis.function.polynomial.root.PolyRoot; |
| 15 | +import com.numericalmethod.suanshu.analysis.function.polynomial.root.PolyRootSolver; |
| 16 | +import com.numericalmethod.suanshu.number.complex.Complex; |
| 17 | + |
| 18 | +class SuanShuMath { |
| 19 | + |
| 20 | + private static final Logger log = LoggerFactory.getLogger(SuanShuMath.class); |
| 21 | + |
| 22 | + public static void main(String[] args) throws Exception { |
| 23 | + SuanShuMath math = new SuanShuMath(); |
| 24 | + |
| 25 | + math.addingVectors(); |
| 26 | + math.scaleVector(); |
| 27 | + math.innerProductVectors(); |
| 28 | + math.addingIncorrectVectors(); |
| 29 | + |
| 30 | + math.addingMatrices(); |
| 31 | + math.multiplyMatrices(); |
| 32 | + math.multiplyIncorrectMatrices(); |
| 33 | + math.inverseMatrix(); |
| 34 | + |
| 35 | + Polynomial p = math.createPolynomial(); |
| 36 | + math.evaluatePolynomial(p); |
| 37 | + math.solvePolynomial(); |
| 38 | + } |
| 39 | + |
| 40 | + public void addingVectors() throws Exception { |
| 41 | + Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5}); |
| 42 | + Vector v2 = new DenseVector(new double[]{5, 4, 3, 2, 1}); |
| 43 | + Vector v3 = v1.add(v2); |
| 44 | + log.info("Adding vectors: {}", v3); |
| 45 | + } |
| 46 | + |
| 47 | + public void scaleVector() throws Exception { |
| 48 | + Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5}); |
| 49 | + Vector v2 = v1.scaled(2.0); |
| 50 | + log.info("Scaling a vector: {}", v2); |
| 51 | + } |
| 52 | + |
| 53 | + public void innerProductVectors() throws Exception { |
| 54 | + Vector v1 = new DenseVector(new double[]{1, 2, 3, 4, 5}); |
| 55 | + Vector v2 = new DenseVector(new double[]{5, 4, 3, 2, 1}); |
| 56 | + double inner = v1.innerProduct(v2); |
| 57 | + log.info("Vector inner product: {}", inner); |
| 58 | + } |
| 59 | + |
| 60 | + public void addingIncorrectVectors() throws Exception { |
| 61 | + Vector v1 = new DenseVector(new double[]{1, 2, 3}); |
| 62 | + Vector v2 = new DenseVector(new double[]{5, 4}); |
| 63 | + Vector v3 = v1.add(v2); |
| 64 | + log.info("Adding vectors: {}", v3); |
| 65 | + } |
| 66 | + |
| 67 | + public void addingMatrices() throws Exception { |
| 68 | + Matrix m1 = new DenseMatrix(new double[][]{ |
| 69 | + {1, 2, 3}, |
| 70 | + {4, 5, 6} |
| 71 | + }); |
| 72 | + |
| 73 | + Matrix m2 = new DenseMatrix(new double[][]{ |
| 74 | + {3, 2, 1}, |
| 75 | + {6, 5, 4} |
| 76 | + }); |
| 77 | + |
| 78 | + Matrix m3 = m1.add(m2); |
| 79 | + log.info("Adding matrices: {}", m3); |
| 80 | + } |
| 81 | + |
| 82 | + public void multiplyMatrices() throws Exception { |
| 83 | + Matrix m1 = new DenseMatrix(new double[][]{ |
| 84 | + {1, 2, 3}, |
| 85 | + {4, 5, 6} |
| 86 | + }); |
| 87 | + |
| 88 | + Matrix m2 = new DenseMatrix(new double[][]{ |
| 89 | + {1, 4}, |
| 90 | + {2, 5}, |
| 91 | + {3, 6} |
| 92 | + }); |
| 93 | + |
| 94 | + Matrix m3 = m1.multiply(m2); |
| 95 | + log.info("Multiplying matrices: {}", m3); |
| 96 | + } |
| 97 | + |
| 98 | + public void multiplyIncorrectMatrices() throws Exception { |
| 99 | + Matrix m1 = new DenseMatrix(new double[][]{ |
| 100 | + {1, 2, 3}, |
| 101 | + {4, 5, 6} |
| 102 | + }); |
| 103 | + |
| 104 | + Matrix m2 = new DenseMatrix(new double[][]{ |
| 105 | + {3, 2, 1}, |
| 106 | + {6, 5, 4} |
| 107 | + }); |
| 108 | + |
| 109 | + Matrix m3 = m1.multiply(m2); |
| 110 | + log.info("Multiplying matrices: {}", m3); |
| 111 | + } |
| 112 | + |
| 113 | + public void inverseMatrix() { |
| 114 | + Matrix m1 = new DenseMatrix(new double[][]{ |
| 115 | + {1, 2}, |
| 116 | + {3, 4} |
| 117 | + }); |
| 118 | + |
| 119 | + Inverse m2 = new Inverse(m1); |
| 120 | + log.info("Inverting a matrix: {}", m2); |
| 121 | + log.info("Verifying a matrix inverse: {}", m1.multiply(m2)); |
| 122 | + } |
| 123 | + |
| 124 | + public Polynomial createPolynomial() { |
| 125 | + return new Polynomial(new double[]{3, -5, 1}); |
| 126 | + } |
| 127 | + |
| 128 | + public void evaluatePolynomial(Polynomial p) { |
| 129 | + // Evaluate using a real number |
| 130 | + log.info("Evaluating a polynomial using a real number: {}", p.evaluate(5)); |
| 131 | + // Evaluate using a complex number |
| 132 | + log.info("Evaluating a polynomial using a complex number: {}", p.evaluate(new Complex(1, 2))); |
| 133 | + } |
| 134 | + |
| 135 | + public void solvePolynomial() { |
| 136 | + Polynomial p = new Polynomial(new double[]{2, 2, -4}); |
| 137 | + PolyRootSolver solver = new PolyRoot(); |
| 138 | + List<? extends Number> roots = solver.solve(p); |
| 139 | + log.info("Finding polynomial roots: {}", roots); |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments