From e06b6cf84b7465622459e3f731ea56e45bb977b2 Mon Sep 17 00:00:00 2001 From: Daniel Weschke Date: Mon, 12 May 2014 11:20:52 +0200 Subject: [PATCH] Add new symmetric matrix class and so a new trinagular number method in Sequence class. Modify constructor and the addition routine in matrix classes. Add a new property to WObjekt to store the number of operations. --- src/math/Maths.java | 12 ++ src/math/Sequence.java | 36 +++++- src/math/Series.java | 3 + src/math/matrix/Diagonal2D.java | 36 ++++++ src/math/matrix/Matrix.java | 168 +++++++++++++++++++-------- src/math/matrix/Matrix2D.java | 1 + src/math/matrix/QRDecomposition.java | 2 +- src/math/matrix/Symmetric.java | 104 +++++++++++++++++ src/math/matrix/TensorII.java | 4 + src/thisandthat/WObject.java | 70 +++++++++-- 10 files changed, 375 insertions(+), 61 deletions(-) create mode 100644 src/math/matrix/Symmetric.java diff --git a/src/math/Maths.java b/src/math/Maths.java index 6af8a60..7b0474f 100644 --- a/src/math/Maths.java +++ b/src/math/Maths.java @@ -778,6 +778,18 @@ final public class Maths{ return sum; } + /** + * @param a + * @return sum of all values in array. + */ + public static Integer sum(Integer[] a) { + int i; + Integer sum = 0; + for(i=0; in = ∑k = 1 + 2 + 3 + 4 + … + n = 0.5*n*(n+1). + * @param n + * @return + */ + public static int triangularNumber(int n){ + return polygonalNumber(3,n); + } + + /** + * Triangular roots. + * @param x + * @return + */ + public static double triangularRoot(int x){ + return (Math.sqrt(8*x+1)-1)/2; + } + + /** + * (s-2)*n*(n-1)/2+n + * @param s + * @param n + * @return + */ + public static int polygonalNumber(int s, int n){ + switch(s){ + case 3: return n*(n+1)/2; + case 4: return n*n; + default: return n*((s-2)*n-s)/2; + } + } /** * Prints out binomial coefficients such that such that a[N][k] contains diff --git a/src/math/Series.java b/src/math/Series.java index dd1758a..32b39ea 100644 --- a/src/math/Series.java +++ b/src/math/Series.java @@ -124,6 +124,7 @@ public class Series{ /** * The sum of a geometric series. + * a+ar+ar2+ar3+… * @param a start term * @param r common ratio * @return sum @@ -136,6 +137,7 @@ public class Series{ /** * The sum of a geometric series. + * a+ar+ar2+ar3+… * @param a start term * @param r common ratio * @return sum @@ -148,6 +150,7 @@ public class Series{ /** * The sum of a geometric series. + * a+ar+ar2+ar3+…+arn-1. * @param a start term * @param r common ratio * @param n first n terms diff --git a/src/math/matrix/Diagonal2D.java b/src/math/matrix/Diagonal2D.java index 9ad7266..283f9d5 100644 --- a/src/math/matrix/Diagonal2D.java +++ b/src/math/matrix/Diagonal2D.java @@ -1,5 +1,7 @@ package math.matrix; +import exception.IllegalDimensionException; + /** * 2D diagonal matrix object dij∈ℝ, i≠j=0 * with 2 rows and 2 columns @@ -17,6 +19,12 @@ public class Diagonal2D extends Matrix2D { n = 2; data = new double[2]; } + + public Diagonal2D(double... i){ + m = 2; + n = 2; + data = i; + } /** * Generate an n-by-n identity matrix. @@ -35,6 +43,34 @@ public class Diagonal2D extends Matrix2D { public void set(int i, double d){ data[i] = d; } + + /** + * Subtract two matrices of the same dimension (entrywise subtraction). + * The subtraction of two m-by-n matrices A and B, + * is again an m-by-n matrix computed by subtracting corresponding elements. + * @param B matrix + * @return C = A - B, entrywise subtracted matrix + * @throws IllegalDimensionException + */ + public Matrix minus(Matrix B) throws IllegalDimensionException{ + checkDimensions(B); + int i,j; + Matrix c = createAddition(B); +// System.out.println(this instanceof Diagonal2D); +// System.out.println(this); +// System.out.println(B instanceof Diagonal2D); +// System.out.println(B); +// System.out.println(c instanceof Diagonal2D); +// System.out.println(c); + if(B instanceof Diagonal) + for(i=0; ii at given index i + * @param i index + * @return the value at the given index i + * @throws IllegalDimensionException + */ + protected double get(int i){ + return data[i]; + } + /** * Get the element Aij at given row i and column j * @param i row @@ -797,7 +817,7 @@ public class Matrix extends WObject implements Cloneable, Serializable{ * @param j column * @param b scalar */ - public void plus(int i, int j, double b){ + protected void plus(int i, int j, double b){ set(i,j, get(i,j)+b); } @@ -812,14 +832,20 @@ public class Matrix extends WObject implements Cloneable, Serializable{ public Matrix plus(Matrix B) throws IllegalDimensionException{ checkDimensions(B); int i,j; - Matrix c = create(this); + Matrix c = createAddition(B); + c.newOperation(); if(B instanceof Diagonal) - for(i=0; i test successful"); + else System.err.println("A*B =? B*A : "+A.times(B).equals(B.times(A)) + " -> test faild"); +// System.out.println(A); + + D = Matrix.random(5, 5); +// D.setName("D").println(); Matrix f = Matrix.random(5, 1); System.out.println("random(5, 1) = \n"+f.setName("f")); - Matrix x = A.solve(f); - System.out.println("A^-1*f = \n"+x.setName("x")); + Matrix x = D.solve(f); + System.out.println("D^-1*f = \n"+x.setName("x")); Matrix2D a = new Matrix2D(0,1,1,0); System.out.println(a.setName("a")); @@ -2413,6 +2480,7 @@ public class Matrix extends WObject implements Cloneable, Serializable{ Vector2D v = new Vector2D(1,1); Vector2D w = new Vector2D(0,1); + System.out.println(v.setName("v")); P = (Matrix2D) Matrix2D.mirror(v); System.out.println("Pw = "); P.times(w).println(); @@ -2487,7 +2555,7 @@ public class Matrix extends WObject implements Cloneable, Serializable{ D = new Diagonal(2.,3); System.out.println(A); - System.out.println(D); + System.out.println(D.setName("D")); System.out.println(A.plus(D)); Matrix I = Matrix.identity(2); diff --git a/src/math/matrix/Matrix2D.java b/src/math/matrix/Matrix2D.java index e11e811..b79076a 100644 --- a/src/math/matrix/Matrix2D.java +++ b/src/math/matrix/Matrix2D.java @@ -214,6 +214,7 @@ public class Matrix2D extends Matrix{ */ public static Matrix2D mirror(Vector v) throws IllegalDimensionException{ // P = I - alpha v v'; alpha = 2 / (v'v) + //System.out.println(Diagonal2D.identity(v.n()) instanceof Diagonal2D); return (Matrix2D) Diagonal2D.identity(v.n()).minus(v.tensorProduct(v).times(2/v.dot(v))); } diff --git a/src/math/matrix/QRDecomposition.java b/src/math/matrix/QRDecomposition.java index 7429dfd..e975a4c 100644 --- a/src/math/matrix/QRDecomposition.java +++ b/src/math/matrix/QRDecomposition.java @@ -18,7 +18,7 @@ import math.Maths; * returns false. * @author Daniel Weschke */ -public class QRDecomposition implements Serializable { +public class QRDecomposition extends Matrix implements Serializable { /** * UID diff --git a/src/math/matrix/Symmetric.java b/src/math/matrix/Symmetric.java new file mode 100644 index 0000000..0dc012c --- /dev/null +++ b/src/math/matrix/Symmetric.java @@ -0,0 +1,104 @@ +package math.matrix; + +import exception.IllegalDimensionException; +import math.Sequence; + +/** + * Symmetric matrix object dij=dji∈ℝ + * with n rows and n columns + * @author Daniel Weschke + */ +public class Symmetric extends Matrix { + + /** + * UID + */ + private static final long serialVersionUID = -3506578822884073555L; + + public Symmetric(int n){ + this.m = n; + this.n = n; + data = new double[Sequence.triangularNumber(n)]; + } + + public Symmetric(double... d){ + this((int)Math.ceil(Sequence.triangularRoot(d.length))); + int i; + for(i=0; ij?Sequence.triangularNumber(i)+j:i+Sequence.triangularNumber(j); + } + + /** + * Adds two matrices of the same dimension (entrywise addition). + * The addition of two n-by-n matrices A and B, + * is again an n-by-n matrix computed by adding corresponding elements. + * @param B matrix + * @return C = A + B, entrywise summarized matrix + * @throws IllegalDimensionException + */ + public Matrix plus(Matrix B) throws IllegalDimensionException{ + checkDimensions(B); + int i,j; + Matrix c = createAddition(B); + c.newOperation(); + if(B instanceof Diagonal) + for(i=0; i operations = new ArrayList(); + private Integer[] convertedArray; + private boolean justConverted; + + public String getName() { + return name; + } + + public WObject setName(String name) { + this.name = name; + return this; + } + + public Integer[] getOperations(){ + if(!justConverted){ + Integer[] itemArray = new Integer[operations.size()]; + convertedArray = operations.toArray(itemArray); + justConverted = true; + } + return convertedArray; + } + + public int getOperationsTotal(){ + return Maths.sum(getOperations()); + } + + public void newOperation(){ + operations.add(0); + justConverted = false; + } + + public void operate(){ + int index = operations.size()-1; + operations.set(index,operations.get(index)+1); + justConverted = false; + } public static boolean isSupressErrorMessage() { return supressErrorMessage; @@ -26,15 +67,6 @@ public class WObject { public static T last(T[] array) { return array[array.length - 1]; } - - public String getName() { - return name; - } - - public WObject setName(String name) { - this.name = name; - return this; - } /** * The declaration of that method is: @@ -67,6 +99,26 @@ public class WObject { WObject obj = new WObject(); obj.setName("one"); obj.println(); + + obj.newOperation(); + System.out.print(Arrays.toString(obj.getOperations())); + System.out.println(" tot: "+obj.getOperationsTotal()); + obj.operate(); + System.out.print(Arrays.toString(obj.getOperations())); + System.out.println(" tot: "+obj.getOperationsTotal()); + obj.operate(); + System.out.print(Arrays.toString(obj.getOperations())); + System.out.println(" tot: "+obj.getOperationsTotal()); + + obj.newOperation(); + System.out.print(Arrays.toString(obj.getOperations())); + System.out.println(" tot: "+obj.getOperationsTotal()); + obj.operate(); + System.out.print(Arrays.toString(obj.getOperations())); + System.out.println(" tot: "+obj.getOperationsTotal()); + obj.operate(); + System.out.print(Arrays.toString(obj.getOperations())); + System.out.println(" tot: "+obj.getOperationsTotal()); } }