projet-rendu/TestAlgebra.java

129 lines
3.4 KiB
Java
Raw Normal View History

2022-04-12 10:08:58 +00:00
import algebra.*;
public class TestAlgebra {
public static void test() throws Exception {
2022-04-14 20:42:57 +00:00
System.out.println("Algebra\n# Test Start");
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
Vector v = new Vector(3);
Vector v1 = new Vector(1);
v1.setName("up");
2022-04-12 10:08:58 +00:00
try {
2022-04-14 20:42:57 +00:00
new Vector(0);
2022-04-12 10:08:58 +00:00
} catch (Exception e) {
2022-04-14 20:42:57 +00:00
System.out.println("Wrong size exception caught OK");
2022-04-12 10:08:58 +00:00
}
try {
2022-04-14 20:42:57 +00:00
new Vector("named", 0);
2022-04-12 10:08:58 +00:00
} catch (Exception e) {
2022-04-14 20:42:57 +00:00
System.out.println("Wrong size exception caught OK");
2022-04-12 10:08:58 +00:00
}
2022-04-14 20:42:57 +00:00
System.out.println(v);
System.out.println(v1);
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
Vector u1 = new Vector("u1", 3);
2022-04-12 10:08:58 +00:00
u1.set(0, 1.0);
u1.set(1, 2.0);
u1.set(2, 3.0);
2022-04-14 20:42:57 +00:00
Vector u2 = new Vector(3);
u2.set(new double[3]);
2022-04-12 10:08:58 +00:00
u2.set(1, 2.0);
2022-04-14 20:42:57 +00:00
System.out.println(u1);
System.out.println("norm(u1) = " + u1.norm());
System.out.println(u2);
System.out.println("u1.u2 = " + u1.dot(u2));
2022-04-12 10:08:58 +00:00
try {
2022-04-14 20:42:57 +00:00
u1 = new Vector("u1", 3);
u2 = new Vector("u2", 4);
u1.dot(u2);
2022-04-12 10:08:58 +00:00
} catch (SizeMismatchException e) {
2022-04-14 20:42:57 +00:00
System.out.println("Caught exception: " + e);
2022-04-12 10:08:58 +00:00
}
2022-04-14 20:42:57 +00:00
Vector3 r1 = new Vector3("u1", 1.0, 2.0, 3.0);
Vector3 r2 = new Vector3("u2", 1.0, 3.0, 0.0);
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
Vector3 r = r1.cross(r2);
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
System.out.println(r1);
System.out.println(r2);
System.out.println("r1.r2 = " + r1.dot(r2));
System.out.println("r1.r2 = " + r1.dot((Vector) r2) + " (as Vector)");
System.out.println("r1 x r2 = " + r);
System.out.println("norm(" + r.getName() + ") = " + r.norm());
r.normalize();
System.out.println("norm(" + r.getName() + ") (after ::normalize()) = " + r.norm());
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
// -------------------------------------
System.out.println("--\n-- Matrix tests\n--");
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
Matrix M = new Matrix(3, 5);
System.out.println(M);
Matrix M1 = new Matrix("M1", 2, 5);
M1.set(0, 0, 2.0);
M1.set(1, 4, -8.0);
M1.set(1, 2, 5.0);
2022-04-12 10:08:58 +00:00
try {
2022-04-14 20:42:57 +00:00
new Matrix("M2", 0, 2);
2022-04-12 10:08:58 +00:00
} catch (Exception e) {
2022-04-14 20:42:57 +00:00
System.out.println("Wrong size exception caught OK");
2022-04-12 10:08:58 +00:00
}
try {
2022-04-14 20:42:57 +00:00
M.multiply(M1);
2022-04-12 10:08:58 +00:00
} catch (SizeMismatchException e) {
2022-04-14 20:42:57 +00:00
System.out.println("Caught exception: " + e);
2022-04-12 10:08:58 +00:00
}
2022-04-14 20:42:57 +00:00
Matrix M2 = Matrix.createIdentity(5);
System.out.println(M2);
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
M = M1.multiply(M2);
System.out.println(M1);
System.out.println(M2);
System.out.println(M);
2022-04-12 10:08:58 +00:00
M1 = M1.transpose();
M1.name = "M1'";
2022-04-14 20:42:57 +00:00
System.out.println(M1);
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
Vector u = new Vector("u", 5);
2022-04-12 10:08:58 +00:00
u.ones();
2022-04-14 20:42:57 +00:00
v = M.multiply(u);
System.out.println(M);
System.out.println(u);
System.out.println("M.u = " + v);
2022-04-12 10:08:58 +00:00
try {
2022-04-14 20:42:57 +00:00
u = new Vector("u", 3);
v = M.multiply(u);
2022-04-12 10:08:58 +00:00
} catch (SizeMismatchException e) {
2022-04-14 20:42:57 +00:00
System.out.println("Caught exception: " + e);
2022-04-12 10:08:58 +00:00
}
2022-04-14 20:42:57 +00:00
Matrix I = Matrix.createIdentity(5);
Matrix S = I.getSubMatrix(0, 2, 3, 3);
System.out.println(I);
System.out.println("submatrix:\n" + S);
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
}
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
public static void main(String[] args) {
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
try {
test();
System.out.println("SUCCESS.");
} catch (Exception e) {
System.out.println("FAIL: " + e);
e.printStackTrace();
}
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
}
2022-04-12 10:08:58 +00:00
}