Skip to content

ojAlgo Fundamentals

Anders Peterson edited this page Mar 21, 2019 · 7 revisions

There is an updated version of this info at: http://www.ojalgo.org/2019/03/linear-algebra-introduction/


A short guide on ojAlgo's various array, vector and matrix classes to help you get started.

The Very Basics

The org.ojalgo.access package contains a number of interfaces widely used throughout ojAlgo, and the root of them all is org.ojalgo.access.Structure1D. This interface only declares one method – count() – returning the total number of elements/items in a data structure. It's equivalent to the size() method of java.util.Collection but returns long rather than int. Some data structures in ojAlgo actually support containing that many elements/items.

In your IDE - assuming you have a project setup with ojAlgo on the classpath - open a type hierarchy with Structure1D at the root. That type hierarchy is fairly deep/complex and encompasses a very large subset of everything in ojAlgo. Understanding at least the basics of how this hierarchy is constructed is necessary for all ojAlgo users - please spend some time exploring it!

Studying that hierarchy you should notice 3 things:

  1. To complement Structure1D there are interfaces Structure2D and StructureAnyD, both of which extend Structure1D. Extending each of those three Structure- interfaces you'll find the Access1D, Access2D and AccessAnyD interfaces. Access2D extends Structure2D AND Access1D, similarly AccessAnyD extends StructureAnyD AND Access1D. This pattern is one of the core design decisions of ojAlgo. Anything "2D" or "AnyD" is also/simultaneously "1D" – there's always a 1D variant of the API. How the 1D and 2D/AnyD API variants correlate is strictly specified, and the AccessUtils class contain methods to help with the translations.
  2. A large portion of the classes/interfaces in ojAlgo use generic type parameters to specify what they contain/handle, and practicly always the generic type declaration is:
    <N extends Number>
    ojAlgo is all about numbers and maths.
  3. The Access1D, Access2D and AccessAnyD interfaces declare methods to get/extract/access one specific elements of a data structure. They each have a get(...) method returning a generic "N" (a Number subclass) as well as a doubleValue(...) method returning primitive double. Whatever Number subclass you're working with you always have direct access to primitive double representations. That can be very handy; particularly when the internal data type actually is primitive double, and that's by far the most commonly used type for maths. This parallel type pattern is another of ojAlgo's core design decisions. To see another example of this, find the org.ojalgo.function.BasicFunction interface and open another type hierarchy. Notice that each of the BasicFunction subtypes have 2 invoke-methods – 1 using primitive double and 1 using a generic "N".

The ojAlgo data structures are not general purpose "collections". They're fixed size and structure, contain numbers only, and primitive double gets special treatment.

Arrays or Matrices?

Assuming you need a general purpose array, vector or matrix implementation of some sort there are two main alternatives:

  1. Array1D, Array2D or ArrayAnyD from the org.ojalgo.array package
  2. Something from the org.ojalgo.matrix package or its sub packages

The classes in the org.ojalgo.array package have three clear advantage over the various matrix implementations:

  1. They can be any-dimensional (AnyD) - matrices are 2D.
  2. They support huge data structures. You really can make use of those long element indices.
  3. They support a larger set of Number subclasses (for instance rational numbers and quaternions).

The key thing not available in the org.ojalgo.array package is "Linear Algebra".