-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
A simple use of Jeometry classes. This document is not exhaustive. Please refer to specific pages for more detailled content.
Jeometry library relies on interfaces for use and builders for instanciation. For the sake of simplicity, all object creation can be done using static methods from the JeometryFactory class (or GeometryFactory class for older version). The following example illustrates how to create basic mathematical objects:
package org.jeometry.sample;
import org.jeometry.factory.JeometryFactory;
import org.jeometry.math.Matrix;
import org.jeometry.math.Quaternion;
import org.jeometry.math.Vector;
/**
* A first sample program.
*/
public class FirstSample {
/**
* The main method.
* @param args program arguments
*/
public static void main(String[] args) {
// Create a 3x3 matrix
Matrix m = JeometryFactory.createMatrix(3, 3);
// Create a vector of dimension 3
Vector v = JeometryFactory.createVector(3);
// Create a quaternion
Quaternion q = JeometryFactory.createQuaternion();
}
}One of the first interest of the Jeometry library is it's mathematical package (org.geometry.math) that provides objects and methods dedicated to computation. The most common objects are Vector, Quaternion and Matrix.
A Jeometry Matrix is the representation of a mathematical matrix, it enables to store real numbers and provide standard computation methods (multiplication, inversion, ...).
A matrix can be instantiated following different ways:
- by specifying its dimensions (rows, columns)
- by initializing with a 2D double array
- by copying an existing matrix
- by initializing with a linear double array (using row / column order)
The following sample illustrates all possible ways.
package org.jeometry.sample.math;
import org.jeometry.factory.JeometryFactory;
import org.jeometry.math.Matrix;
/**
* A matrix creation sample.
*/
public class MatrixCreationSample {
/**
* The main method.
* @param args the program arguments
*/
public static void main(String[] args) {
// 1. Matrix creation using dimensions.
Matrix a = JeometryFactory.createMatrix(3, 3);
// 2. Matrix creation using 2D array
double[][] bdata = new double[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Matrix b = JeometryFactory.createMatrix(bdata);
// 3. Matrix creation by copy
Matrix c = JeometryFactory.createMatrix(b);
// 4. Matrix creation using a linear array, row major and column major
double[] ddata = new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
Matrix d = JeometryFactory.createMatrix(3, 3, ddata, Matrix.ROW_MAJOR);
Matrix e = JeometryFactory.createMatrix(3, 3, ddata, Matrix.COLUMN_MAJOR);
}
}Standard operations on matrices are implemented. The first operations are related to matrix data access as illustrated within the following example:
package org.jeometry.sample.math;
import org.jeometry.factory.JeometryFactory;
import org.jeometry.math.Matrix;
/**
* Matrix operations samples.
*/
public class MatrixDataOperationsSample {
/**
* The main method.
* @param args the program arguments
*/
public static void main(String[] args) {
//Matrix creation using 2D array
double[][] bdata = new double[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Matrix a = JeometryFactory.createMatrix(bdata);
// Matrix creation using dimension
Matrix b = JeometryFactory.createMatrix(4, 3);
// Display matrix dimension
System.out.println("A["+a.getRowsCount()+"x"+a.getColumnsCount()+"]");
// Get matrix value
System.out.println("A[2x3] = "+a.getValue(2, 3));
// Set matrix value
for(int row = 0; row < b.getRowsCount(); row++) {
for(int col = 0; col < b.getColumnsCount(); col++) {
b.setValue(row, col, row*col);
}
}
}
}Mathematical operations on matrices are also available. There is 3 groups of operations:
- Addition / subtraction
- Multiplication
- Transposition / inversion
package org.jeometry.sample.math;
import org.jeometry.factory.JeometryFactory;
import org.jeometry.math.Matrix;
/**
* Matrix mathematical operations sample.
*/
public class MatrixMathOperationsSample {
/**
* The main method.
* @param args the program arguments
*/
public static void main(String[] args) {
Matrix a = JeometryFactory.createMatrix(new double[][] {
{ 3.45692302, -1.36528596, 1.3685204, -459.0254136},
{22.65974148, 0.00698741, 8.1269853, 23.5410397},
{12.87456921, -3.12586921, 11.3685214, -33.2154242},
{36.25697942, -3.01127952, 6984.3652127, 12.6985412}
});
Matrix b = JeometryFactory.createMatrix(new double[][] {{6, 5, 4, 1}, {9, 8, 7, 2}, {3, 2, 1, 5}, {10, 11, 12, 4}});
// Add the scalar 5.0 to all matrix cells
Matrix ap = a.add(5.0);
// Create a new matrix where each cell is the addition of the corresponding
// cells from matrix a and b
Matrix apb = a.add(b);
// Subtract the scalar 4.0 from all matrix cells
Matrix am = a.subtract(4.0);
// Create a new matrix where each cell is the subtraction of the corresponding
// cells from matrix a and b
Matrix amb = a.subtract(b);
// Multiply all matrix cells by the scalar 2.0
Matrix ax = a.multiply(2.0);
// The standard matrix product a x b
Matrix axb = a.multiply(amb);
// Transpose the matrix
Matrix t = a.transpose();
// Invert the matrix
Matrix i = a.invert();
}
}All operations propose different ways of use. In most of the case, for a specific operation, 3 methods are available:
- A method that return the result as a new object
- A method that set the result within a given pre-instanciated parameter
- A method that modyfy the object itself