001/* 002 * $Id$ 003 */ 004 005package edu.jas.commons.math; 006 007 008import org.apache.commons.math3.linear.FieldDecompositionSolver; 009import org.apache.commons.math3.linear.FieldLUDecomposition; 010import org.apache.commons.math3.linear.FieldMatrix; 011import org.apache.commons.math3.linear.FieldVector; 012 013import edu.jas.structure.RingElem; 014import edu.jas.vector.GenMatrix; 015import edu.jas.vector.GenVector; 016 017 018/** 019 * Algorithms related to Gaussian elimination. Conversion to commons-math 020 * classes and delegation to commons-math algorithms. 021 * @param <C> ring element type 022 * @author Heinz Kredel 023 */ 024 025public class GaussElimination<C extends RingElem<C>> { 026 027 028 /** 029 * Determinant of a matrix. 030 * @param a matrix 031 * @return determinant of a 032 */ 033 public C determinant(GenMatrix<C> a) { 034 FieldMatrix<CMFieldElement<C>> am = CMFieldElementUtil.<C> toCMFieldMatrix(a); 035 036 final FieldLUDecomposition<CMFieldElement<C>> lu = new FieldLUDecomposition<CMFieldElement<C>>(am); 037 CMFieldElement<C> dm = lu.getDeterminant(); 038 C d = dm.val; 039 return d; 040 } 041 042 043 /** 044 * Inverse of a matrix. 045 * @param a matrix 046 * @return inverse matrix of a 047 */ 048 public GenMatrix<C> inverse(GenMatrix<C> a) { 049 FieldMatrix<CMFieldElement<C>> am = CMFieldElementUtil.<C> toCMFieldMatrix(a); 050 final FieldLUDecomposition<CMFieldElement<C>> lu = new FieldLUDecomposition<CMFieldElement<C>>(am); 051 FieldDecompositionSolver<CMFieldElement<C>> fds = lu.getSolver(); 052 FieldMatrix<CMFieldElement<C>> bm = fds.getInverse(); 053 GenMatrix<C> g = CMFieldElementUtil.<C> matrixFromCMFieldMatrix(a.ring, bm); 054 return g; 055 } 056 057 058 /** 059 * Test if n is a null space for the linear system: a * n = 0. 060 * @param a matrix 061 * @param n matrix 062 * @return true, if n is a nullspace of a, else false 063 */ 064 public boolean isNullSpace(GenMatrix<C> a, GenMatrix<C> n) { 065 GenMatrix<C> z = a.multiply(n); // .transpose(n.ring) better not transpose 066 // here 067 // System.out.println("z = " + z); 068 return z.isZERO(); 069 } 070 071 072 /** 073 * Solve a linear system: a x = b. 074 * @param a matrix 075 * @param b vector of right hand side 076 * @return a solution vector x 077 */ 078 public GenVector<C> solve(GenMatrix<C> a, GenVector<C> b) { 079 FieldMatrix<CMFieldElement<C>> am = CMFieldElementUtil.<C> toCMFieldMatrix(a); 080 FieldVector<CMFieldElement<C>> bv = CMFieldElementUtil.<C> toCMFieldElementVector(b); 081 082 final FieldLUDecomposition<CMFieldElement<C>> lu = new FieldLUDecomposition<CMFieldElement<C>>(am); 083 FieldDecompositionSolver<CMFieldElement<C>> fds = lu.getSolver(); 084 FieldVector<CMFieldElement<C>> xv = fds.solve(bv); 085 GenVector<C> xa = CMFieldElementUtil.<C> vectorFromCMFieldVector(b.modul, xv); 086 return xa; 087 } 088 089 090 /** 091 * Trace of a matrix. 092 * @param a matrix 093 * @return trace of a 094 */ 095 public C trace(GenMatrix<C> a) { 096 FieldMatrix<CMFieldElement<C>> am = CMFieldElementUtil.<C> toCMFieldMatrix(a); 097 CMFieldElement<C> dm = am.getTrace(); 098 C d = dm.val; 099 return d; 100 } 101 102}