001/* 002 * $Id$ 003 */ 004 005package edu.jas.jlinalg; 006 007 008import org.jlinalg.LinSysSolver; 009import org.jlinalg.Matrix; 010import org.jlinalg.Vector; 011import org.jlinalg.polynomial.Polynomial; 012 013import edu.jas.arith.BigRational; 014import edu.jas.poly.GenPolynomial; 015import edu.jas.vector.GenMatrix; 016import edu.jas.vector.GenMatrixRing; 017import edu.jas.vector.GenVector; 018import edu.jas.vector.GenVectorModul; 019 020 021/** 022 * Example that computes a solution of a linear equation system. 023 * @author Heinz Kredel 024 */ 025 026public class MatrixExamples { 027 028 029 public static void main(String[] argv) { 030 example1(); 031 example2(); 032 example3(); 033 } 034 035 036 @SuppressWarnings({ "unchecked" }) 037 public static void example1() { 038 BigRational r1, r2, r3, r4, r5, r6, z; 039 r1 = new BigRational(1, 10); 040 r2 = new BigRational(6, 5); 041 r3 = new BigRational(1, 9); 042 r4 = new BigRational(1, 1); 043 r5 = r2.sum(r3); 044 r6 = r1.multiply(r4); 045 z = new BigRational(); 046 047 JLAdapter<BigRational> r1a, r2a, r3a, r4a, r5a, r6a, d, t, za; 048 r1a = new JLAdapter<BigRational>(r1); 049 r2a = new JLAdapter<BigRational>(r2); 050 r3a = new JLAdapter<BigRational>(r3); 051 r4a = new JLAdapter<BigRational>(r4); 052 r5a = new JLAdapter<BigRational>(r5); 053 r6a = new JLAdapter<BigRational>(r6); 054 za = new JLAdapter<BigRational>(z); 055 056 Matrix<JLAdapter<BigRational>> a = new Matrix<JLAdapter<BigRational>>( 057 new JLAdapter[][] { { r1a, r2a, r3a }, { r4a, r5a, r6a } }); 058 059 System.out.println("system = " + a); 060 061 Vector<JLAdapter<BigRational>> b = new Vector<JLAdapter<BigRational>>(new JLAdapter[] { r1a, r2a }); 062 System.out.println("rhs = " + b); 063 064 Vector<JLAdapter<BigRational>> solution = LinSysSolver.solve(a, b); 065 System.out.println("x = " + solution); 066 067 System.out.println("solutionSpace = " + LinSysSolver.solutionSpace(a, b)); 068 Vector<JLAdapter<BigRational>> bz = new Vector<JLAdapter<BigRational>>(new JLAdapter[] { za, za }); 069 System.out.println("rhs = " + bz); 070 System.out.println("nullSolutionSpace = " + LinSysSolver.solutionSpace(a, bz)); 071 072 Matrix<JLAdapter<BigRational>> m = new Matrix<JLAdapter<BigRational>>( 073 new JLAdapter[][] { { r1a, r2a, r3a }, { r4a, r5a, r6a }, { r2a, r1a, r3a } }); 074 System.out.println("matrix = " + m); 075 076 m = m.multiply(m); 077 System.out.println("matrix^2 = " + m); 078 079 d = m.det(); 080 System.out.println("det(matrix^2) = " + d); 081 082 t = m.trace(); 083 System.out.println("trace(matrix^2) = " + t); 084 085 Polynomial<JLAdapter<BigRational>> cp = m.characteristicPolynomial(); 086 System.out.println("charPol(matrix^2) = " + cp); 087 088 Polynomial<JLAdapter<BigRational>> mp = m.minimalPolynomial(); 089 System.out.println("minPol(matrix^2) = " + mp); 090 091 Matrix<JLAdapter<BigRational>> mg = m.gausselim(); 092 System.out.println("matrix, gauss = " + mg); 093 094 Matrix<JLAdapter<BigRational>> m2 = new Matrix<JLAdapter<BigRational>>(new JLAdapter[][] { 095 { r1a, r2a, r3a, r4a }, { r4a, r5a, r6a, r1a }, { r2a, r1a, r3a, r5a } }); 096 System.out.println("matrix_2 = " + m2); 097 Matrix<JLAdapter<BigRational>> mj = m2.gaussjord(); 098 System.out.println("matrix,gauss-jordan = " + mj); 099 100 try { 101 System.out.println("matrix,eigenvalue = "); // + m.eig()); fails for Double 102 } catch (org.jlinalg.InvalidOperationException e) { 103 System.out.println("" + e); 104 } 105 } 106 107 108 @SuppressWarnings({ "unchecked" }) 109 public static void example2() { 110 BigRational r1, r2, r3, r4, r5, r6, z; 111 r1 = new BigRational(1, 10); 112 r2 = new BigRational(6, 5); 113 r3 = new BigRational(1, 9); 114 r4 = new BigRational(1, 1); 115 r5 = r2.sum(r3); 116 r6 = r1.multiply(r4); 117 z = new BigRational(); 118 119 JLAdapter<BigRational> d, t; 120 121 Matrix<JLAdapter<BigRational>> a = new Matrix<JLAdapter<BigRational>>(JLAdapterUtil 122 .<BigRational> toJLAdapter(new BigRational[][] { { r1, r2, r3 }, { r4, r5, r6 } })); 123 System.out.println("system = " + a); 124 125 Vector<JLAdapter<BigRational>> b = new Vector<JLAdapter<BigRational>>( 126 JLAdapterUtil.<BigRational> toJLAdapter(new BigRational[] { r1, r2 })); 127 System.out.println("rhs = " + b); 128 129 Vector<JLAdapter<BigRational>> solution = LinSysSolver.solve(a, b); 130 System.out.println("x = " + solution); 131 132 System.out.println("solutionSpace = " + LinSysSolver.solutionSpace(a, b)); 133 134 Vector<JLAdapter<BigRational>> bz = new Vector<JLAdapter<BigRational>>( 135 JLAdapterUtil.<BigRational> toJLAdapter(new BigRational[] { z, z })); 136 System.out.println("rhs = " + bz); 137 System.out.println("nullSolutionSpace = " + LinSysSolver.solutionSpace(a, bz)); 138 139 Matrix<JLAdapter<BigRational>> m = new Matrix<JLAdapter<BigRational>>( 140 JLAdapterUtil.<BigRational> toJLAdapter(new BigRational[][] { { r1, r2, r3 }, 141 { r4, r5, r6 }, { r2, r1, r3 } })); 142 System.out.println("matrix = " + m); 143 144 m = m.multiply(m); 145 System.out.println("matrix^2 = " + m); 146 147 d = m.det(); 148 System.out.println("det(matrix^2) = " + d); 149 150 t = m.trace(); 151 System.out.println("trace(matrix^2) = " + t); 152 153 Polynomial<JLAdapter<BigRational>> cp = m.characteristicPolynomial(); 154 System.out.println("charPol(matrix^2) = " + cp); 155 156 Polynomial<JLAdapter<BigRational>> mp = m.minimalPolynomial(); 157 System.out.println("minPol(matrix^2) = " + mp); 158 159 Matrix<JLAdapter<BigRational>> mg = m.gausselim(); 160 System.out.println("matrix, gauss = " + mg); 161 162 Matrix<JLAdapter<BigRational>> m2 = new Matrix<JLAdapter<BigRational>>( 163 JLAdapterUtil.<BigRational> toJLAdapter(new BigRational[][] { { r1, r2, r3, r4 }, 164 { r4, r5, r6, r1 }, { r2, r1, r3, r5 } })); 165 System.out.println("matrix_2 = " + m2); 166 Matrix<JLAdapter<BigRational>> mj = m2.gaussjord(); 167 System.out.println("matrix,gauss-jordan = " + mj); 168 169 try { 170 System.out.println("matrix,eigenvalue = "); // + m.eig()); fails for Double 171 } catch (org.jlinalg.InvalidOperationException e) { 172 System.out.println("" + e); 173 } 174 } 175 176 177 @SuppressWarnings({ "unchecked" }) 178 public static void example3() { 179 BigRational r1, r2, r3, r4, r5, r6, fac; 180 r1 = new BigRational(1, 10); 181 r2 = new BigRational(6, 5); 182 r3 = new BigRational(1, 9); 183 r4 = new BigRational(1, 1); 184 r5 = r2.sum(r3); 185 r6 = r1.multiply(r4); 186 187 fac = new BigRational(); 188 189 //JLAdapter<BigRational> d, t; 190 191 BigRational[][] aa = new BigRational[][] { { r1, r2, r3 }, { r4, r5, r6 } }; 192 GenMatrixRing<BigRational> mfac = new GenMatrixRing<BigRational>(fac, aa.length, aa[0].length); 193 GenMatrix<BigRational> a = new GenMatrix<BigRational>(mfac, JLAdapterUtil.<BigRational> toList(aa)); 194 System.out.println("system = " + a); 195 196 BigRational[] ba = new BigRational[] { r1, r2 }; 197 GenVectorModul<BigRational> vfac = new GenVectorModul<BigRational>(fac, ba.length); 198 GenVector<BigRational> b = new GenVector<BigRational>(vfac, JLAdapterUtil.<BigRational> toList(ba)); 199 System.out.println("right hand side = " + b); 200 201 GaussElimination<BigRational> ge = new GaussElimination<BigRational>(); 202 GenVector<BigRational> x = ge.solve(a, b); 203 System.out.println("solution = " + x); 204 205 BigRational[][] am = new BigRational[][] { { r1, r2, r3 }, { r4, r5, r6 }, { r2, r1, r3 } }; 206 GenMatrixRing<BigRational> mfacm = new GenMatrixRing<BigRational>(fac, am.length, am[0].length); 207 GenMatrix<BigRational> m = new GenMatrix<BigRational>(mfacm, JLAdapterUtil.<BigRational> toList(am)); 208 System.out.println("matrix = " + m); 209 GenPolynomial<BigRational> p = ge.characteristicPolynomial(m); 210 System.out.println("characteristicPolynomial = " + p); 211 212 // m = m.multiply(m); 213 // System.out.println("matrix = " + m); 214 // p = ge.characteristicPolynomial(m); 215 // System.out.println("characteristicPolynomial = " + p); 216 217 m = mfacm.getONE(); 218 System.out.println("matrix = " + m); 219 p = ge.characteristicPolynomial(m); 220 System.out.println("characteristicPolynomial = " + p); 221 222 m = mfacm.getZERO(); 223 System.out.println("matrix = " + m); 224 p = ge.characteristicPolynomial(m); 225 System.out.println("characteristicPolynomial = " + p); 226 227 m = mfacm.getONE(); 228 System.out.println("matrix = " + m); 229 GenMatrix<BigRational> ns = ge.nullSpace(m); 230 System.out.println("nullSpace = " + ns); 231 System.out.println("isNullSpace = " + ge.isNullSpace(m, ns)); 232 233 am = new BigRational[][] { { r1, r2, r3 }, { r4, r5, r6 }, { r4, r5, r6 } }; 234 mfacm = new GenMatrixRing<BigRational>(fac, am.length, am[0].length); 235 m = new GenMatrix<BigRational>(mfacm, JLAdapterUtil.<BigRational> toList(am)); 236 System.out.println("matrix = " + m); 237 238 ns = ge.nullSpace(m); 239 System.out.println("nullSpace = " + ns); 240 System.out.println("isNullSpace = " + ge.isNullSpace(m, ns)); 241 //System.out.println("isNullSpace = " + ge.isNullSpace(ns,m)); 242 //System.out.println("isNullSpace = " + ge.isNullSpace(m,ns.transpose(mfacm))); 243 244 m = mfacm.getZERO(); 245 System.out.println("matrix = " + m); 246 ns = ge.nullSpace(m); 247 System.out.println("nullSpace = " + ns); 248 System.out.println("isNullSpace = " + ge.isNullSpace(m, ns)); 249 250 int kl = 3; 251 int ll = 10; 252 mfac = new GenMatrixRing<BigRational>(fac, ll, ll + 1); 253 m = mfac.random(kl); 254 System.out.println("matrix = " + m); 255 m = ge.gaussElimination(m); 256 System.out.println("Gauss elimination = " + m); 257 258 m = ge.gaussJordanElimination(m); 259 System.out.println("Gauss-Jordan elimination = " + m); 260 261 mfac = new GenMatrixRing<BigRational>(fac, ll, ll); 262 m = mfac.random(kl); 263 System.out.println("matrix = " + m); 264 GenMatrix<BigRational> mi = null; 265 try { 266 mi = ge.inverse(m); 267 System.out.println("inverse = " + mi); 268 if (mi != null) { 269 GenMatrix<BigRational> tt = m.multiply(mi); 270 boolean inv = tt.isONE(); 271 if (!inv) { 272 System.out.println("m * inverse = " + tt); 273 } 274 System.out.println("isInverse = " + inv); 275 } 276 } catch (Exception ignored) { 277 } 278 kl = 3; 279 ll = 10; // for ll > 40 must adjust JVM parameters, e.g. -Xms500M -Xmx600M 280 mfac = new GenMatrixRing<BigRational>(fac, ll, ll); 281 m = mfac.random(kl); 282 //System.out.println("matrix = " + m); 283 m = ge.gaussElimination(m); 284 //System.out.println("Gauss elimination = " + m); 285 286 m = ge.gaussJordanElimination(m); 287 //System.out.println("Gauss-Jordan elimination = " + m); 288 System.out.println("is Gauss-Jordan elimination = " + m.isONE()); 289 } 290 291}