vkostyukov / la4j

Linear Algebra for Java
http://la4j.org
Apache License 2.0
373 stars 107 forks source link

Use pipeTo instead of with? #179

Open vkostyukov opened 10 years ago

vkostyukov commented 10 years ago

I'm thinking of using pipeTo method name istead of withSolver, withDecompositor, withInverter:

Matrix {
  LinearSystemSolver pipeTo(SolverFactory factory);
  MatrixInverter pipeTo(InverterFactory factory);
  MatrixDecompisitor pipeTo(DecompositorFactory factory);
}
vkostyukov commented 10 years ago

An example of usage:

Matrix a = ...
Matrix b = a.pipeTo(LinearAlgebra.GAUSS_JORDAN).invert();
vkostyukov commented 10 years ago
Matrix a = ...
// GAUSS_JORDAN is no longe a factory in an operation from Matrix to MatrixInverter
MatrixInverter = a.pipeTo(LinearAlgebra.GAUSS_JORDAN);
// A typical use-case
Matrix b = a.pipeTo(LinearAlgebra.GAUSS_JORDAN).invert();
// Or in-place:
Matrix b = a.pipeTo(LinearAlgebra.GAUSS_JORDAN).invertInPlace();
// Or do it in a single line
Matrix b = a.pipeAndApplyTo(LinearAlgebra.GAUSS_JORDAN);
// Or in-place
Matrix b = a.pipeAndApplyInPlaceTo(LinearAlgebra.GAUSS_JORDAN);
vkostyukov commented 10 years ago

Everything is an operation: Iverter, Decompisitor, LinearSolver. And pipeTo is a core routin to work with it. This should be refactored in this release. For now, only oo-place operations for complex things.

vkostyukov commented 10 years ago

Just curring:

// we didn't pass vector to a MatrixVectorOration, so it returned a curried operation;
// we still have troubles here since we want to cache the LU between calls to solver.apply().
VectorOperation<Vector> solver = a.pipeTo(LinearAlgebra.FORWARD_BACK_SUBST);
solver.apply(Vector);
// we might want to pass a factory
Matrix b = a.pipeTo(LinearAlgebra.GAUSS_JORDAN);

// GAUSS_JORDAN is an operation factory
vkostyukov commented 10 years ago

A matrix-operation-factory concept

MatrixOperationFactory<T> {
  MatrixOperation<T> apply(Factory factory)
}
vkostyukov commented 10 years ago

In a base class:

public <T> pipeTo<T>(MatrixOperationFactory<T> operation) {
  return pipeTo(operation.apply(factory))
}
vkostyukov commented 10 years ago

The overall picture for decomposers should looks like:

Matrix lup[] = a.pipeTo(LinearAlgebra.LU);