Skip to content

Commit 831cdf2

Browse files
pvangoolmaibin
authored andcommitted
BAEL-1215: Introduction to the Suanshu math library (eugenp#5469)
* BAEL-1215: Introduction to the Suanshu math library * Updated examples based on feedback.
1 parent 03065a4 commit 831cdf2

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,12 @@
711711
<version>${snakeyaml.version}</version>
712712
</dependency>
713713

714+
<dependency>
715+
<groupId>com.numericalmethod</groupId>
716+
<artifactId>suanshu</artifactId>
717+
<version>${suanshu.version}</version>
718+
</dependency>
719+
714720
</dependencies>
715721

716722
<repositories>
@@ -731,6 +737,12 @@
731737
<id>Apache Staging</id>
732738
<url>https://repository.apache.org/content/groups/staging</url>
733739
</repository>
740+
<repository>
741+
<id>nm-repo</id>
742+
<name>Numerical Method's Maven Repository</name>
743+
<url>http://repo.numericalmethod.com/maven/</url>
744+
<layout>default</layout>
745+
</repository>
734746
</repositories>
735747

736748

@@ -835,6 +847,7 @@
835847
</build>
836848

837849
<properties>
850+
<suanshu.version>4.0.0</suanshu.version>
838851
<snakeyaml.version>1.21</snakeyaml.version>
839852
<googleclient.version>1.23.0</googleclient.version>
840853
<crdt.version>0.1.0</crdt.version>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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

Comments
 (0)