Difference between revisions of "Example Fixed Sized Matrices"

From Efficient Java Matrix Library
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Array access adds a significant amount of overhead to matrix operations.  A fixed sized matrix gets around that issue by having each element in the matrix be a variable in the class.  EJML provides support for fixed sized matrices and vectors up to 6x6, at which point it loses its advantage.  The example below demonstrates how to use a fixed sized matrix and convert to other matrix types in EJML.
 
Array access adds a significant amount of overhead to matrix operations.  A fixed sized matrix gets around that issue by having each element in the matrix be a variable in the class.  EJML provides support for fixed sized matrices and vectors up to 6x6, at which point it loses its advantage.  The example below demonstrates how to use a fixed sized matrix and convert to other matrix types in EJML.
  
Code on GitHub:
+
External Resources:
* [https://github.com/lessthanoptimal/ejml/blob/v0.27/examples/src/org/ejml/example/ExampleFixedSizedMatrix.java ExampleFixedSizedMatrix]
+
* [https://github.com/lessthanoptimal/ejml/blob/v0.41/examples/src/org/ejml/example/ExampleFixedSizedMatrix.java ExampleFixedSizedMatrix]
 +
* <disqus>Discuss this example</disqus>
  
 
== Example ==
 
== Example ==
Line 15: Line 16:
 
  */
 
  */
 
public class ExampleFixedSizedMatrix {
 
public class ExampleFixedSizedMatrix {
 
+
     public static void main( String[] args ) {
     public static void main( String args[] ) {
 
 
         // declare the matrix
 
         // declare the matrix
         FixedMatrix3x3_64F a = new FixedMatrix3x3_64F();
+
         DMatrix3x3 a = new DMatrix3x3();
         FixedMatrix3x3_64F b = new FixedMatrix3x3_64F();
+
         DMatrix3x3 b = new DMatrix3x3();
  
 
         // Can assign values the usual way
 
         // Can assign values the usual way
         for( int i = 0; i < 3; i++ ) {
+
         for (int i = 0; i < 3; i++) {
             for( int j = 0; j < 3; j++ ) {
+
             for (int j = 0; j < 3; j++) {
                 a.set(i,j,i+j+1);
+
                 a.set(i, j, i + j + 1);
 
             }
 
             }
 
         }
 
         }
Line 36: Line 36:
  
 
         // most of the standard operations are support
 
         // most of the standard operations are support
         FixedOps3.transpose(a,b);
+
         CommonOps_DDF3.transpose(a, b);
 
         b.print();
 
         b.print();
  
         System.out.println("Determinant = "+FixedOps3.det(a));
+
         System.out.println("Determinant = " + CommonOps_DDF3.det(a));
  
 
         // matrix-vector operations are also supported
 
         // matrix-vector operations are also supported
 
         // Constructors for vectors and matrices can be used to initialize its value
 
         // Constructors for vectors and matrices can be used to initialize its value
         FixedMatrix3_64F v = new FixedMatrix3_64F(1,2,3);
+
         DMatrix3 v = new DMatrix3(1, 2, 3);
         FixedMatrix3_64F result = new FixedMatrix3_64F();
+
         DMatrix3 result = new DMatrix3();
  
         FixedOps3.mult(a,v,result);
+
         CommonOps_DDF3.mult(a, v, result);
  
         // Conversion into DenseMatrix64F can also be done
+
         // Conversion into DMatrixRMaj can also be done
         DenseMatrix64F dm = ConvertMatrixType.convert(a,null);
+
         DMatrixRMaj dm = DConvertMatrixStruct.convert(a, null);
  
 
         dm.print();
 
         dm.print();
Line 57: Line 57:
  
 
         // can then convert it back into a fixed matrix
 
         // can then convert it back into a fixed matrix
         FixedMatrix3x3_64F fv = ConvertMatrixType.convert(sv.getMatrix(),(FixedMatrix3x3_64F)null);
+
         DMatrix3x3 fv = DConvertMatrixStruct.convert(sv.getDDRM(), (DMatrix3x3)null);
  
 
         System.out.println("Original simple matrix and converted fixed matrix");
 
         System.out.println("Original simple matrix and converted fixed matrix");

Latest revision as of 07:37, 7 July 2021

Array access adds a significant amount of overhead to matrix operations. A fixed sized matrix gets around that issue by having each element in the matrix be a variable in the class. EJML provides support for fixed sized matrices and vectors up to 6x6, at which point it loses its advantage. The example below demonstrates how to use a fixed sized matrix and convert to other matrix types in EJML.

External Resources:

Example

/**
 * In some applications a small fixed sized matrix can speed things up a lot, e.g. 8 times faster.  One application
 * which uses small matrices is graphics and rigid body motion, which extensively uses 3x3 and 4x4 matrices.  This
 * example is to show some examples of how you can use a fixed sized matrix.
 *
 * @author Peter Abeles
 */
public class ExampleFixedSizedMatrix {
    public static void main( String[] args ) {
        // declare the matrix
        DMatrix3x3 a = new DMatrix3x3();
        DMatrix3x3 b = new DMatrix3x3();

        // Can assign values the usual way
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                a.set(i, j, i + j + 1);
            }
        }

        // Direct manipulation of each value is the fastest way to assign/read values
        a.a11 = 12;
        a.a23 = 64;

        // can print the usual way too
        a.print();

        // most of the standard operations are support
        CommonOps_DDF3.transpose(a, b);
        b.print();

        System.out.println("Determinant = " + CommonOps_DDF3.det(a));

        // matrix-vector operations are also supported
        // Constructors for vectors and matrices can be used to initialize its value
        DMatrix3 v = new DMatrix3(1, 2, 3);
        DMatrix3 result = new DMatrix3();

        CommonOps_DDF3.mult(a, v, result);

        // Conversion into DMatrixRMaj can also be done
        DMatrixRMaj dm = DConvertMatrixStruct.convert(a, null);

        dm.print();

        // This can be useful if you need do more advanced operations
        SimpleMatrix sv = SimpleMatrix.wrap(dm).svd().getV();

        // can then convert it back into a fixed matrix
        DMatrix3x3 fv = DConvertMatrixStruct.convert(sv.getDDRM(), (DMatrix3x3)null);

        System.out.println("Original simple matrix and converted fixed matrix");
        sv.print();
        fv.print();
    }
}