16  Intro to Linear Algebra - DRAFT πŸ› 

In this tutorial, we introduce basic linear algebra concepts and the way they can be computed using Fastmath.

16.1 What is it about?

Linear algebra focuses on vectors and certain kinds of transformations of them, called linear transformations.

In our context here, our vectors are ordered collections of floating-point numbers.

This is a concrete special case of a more absract and more general notion of elements of vector spaces. As usual, abstraction can be useful for our reasoning. We recommend learning abouthe more general ideas of linear algebra. Probably, for Clojurians who appreciate simplicity and functional composition, those ideas can be attractive.

Linear transformations are transformations which are simple, in a certain sense which can be made precise. They are often useful when mixed and composed with nonlinear transformations, and of course, the Fastmath API offers both kinds, as we will see below.

Implementation-wise, vectors can be represented in many ways. Clojure’s persistent vectors are one way (assuming they contain only numbers). It is a bit heavy in time and space, since all numbers are boxed in objects. Java arrays are a more lightweight representatin.

16.3 Setup

(ns noj-book.linear-algebra-intro
  (:require
   [clojure.math :as math]
   [fastmath.vector :as vec]
   [scicloj.kindly.v4.api :as kindly]))

util vars

(def java-double-array (double-array 5))

16.4 What is a linear transformation?

(coming soon)

16.5 Addition

(vec/add [1 9]
         [0 -3])
[1.0 6.0]

16.6 Scalar multiplication

(vec/mult [1 9]
          1000)
[1000.0 9000.0]

16.7 Subtraction

When we pass single vector to fastmath/sub function it will be multiplied by -1.0.

(vec/sub [10 5])
[-10.0 -5.0]

When we pass two vectors to fastmath/sub function it will perform basic vector subtraction.

(vec/sub [10 5]
         [8 4])
[2.0 1.0]

16.8 Dot product

When we pass two vectors to fastmath/dot function it will perform dot product operation on them. a Β· b = a₁b₁ + aβ‚‚bβ‚‚ + … + anbn

(vec/dot [10 5]
         [8 4])
100.0

16.9 Converters

16.9.1 Vector to Java array of doubles [D.

(vec/vec->array [10 5])
[10.0, 5.0]
(type (vec/vec->array [10 5]))
double/1

16.9.2 Java array to Clojure sequence.

(identity java-double-array)
[0.0, 0.0, 0.0, 0.0, 0.0]
(type (vec/vec->seq java-double-array))
clojure.lang.ArraySeq$ArraySeq_double

16.9.3 Vector or Java array to Apache Commons Math RealVector.

(type (vec/vec->RealVector [10 5]))
org.apache.commons.math3.linear.ArrayRealVector
(identity java-double-array)
[0.0, 0.0, 0.0, 0.0, 0.0]
(type (vec/vec->RealVector java-double-array))
org.apache.commons.math3.linear.ArrayRealVector

16.9.4 Clojure vector or Java array to primitive vector Vec.

(vec/vec->Vec [10 5])
[10.0 5.0]
(type (vec/vec->Vec [10 5]))
clojure.core.Vec
(identity java-double-array)
[0.0, 0.0, 0.0, 0.0, 0.0]
(type (vec/vec->Vec java-double-array))
clojure.core.Vec

16.9.5 WIP – if two vectors are passed it takes count of elemets from first vec and

returns same count of elements from second vec??

(vec/as-vec [10 2] [5 10 15])
[5 10]

16.9.6 WIP – if one vector is passed it takes count of elemets from vec and returns same

count of elemets from new vector with each value being 0,0.

(vec/as-vec [5 10 15])
[0.0 0.0 0.0]

Magnitude of the vector calculated using Pythagoras(norm).

(vec/mag [3 4])
5.0

Round the value from the vector based on second argument

(vec/approx [math/PI])
[3.14]
(vec/approx [math/PI math/PI math/PI] 5)
[3.14159 3.14159 3.14159]

Equality tolerance β€œElement-wise equality with given absolute (and/or relative) tolerance.”

(vec/edelta-eq [math/PI] (vec/approx [math/PI] 4))
false
(vec/edelta-eq [math/PI] (vec/approx [math/PI] 4) 0.001)
true
(vec/edelta-eq [math/PI] (vec/approx [math/PI] 4) 0.000001)
false

Equality with given absolute (and/or relative) tolerance.

(vec/delta-eq [math/PI] (vec/approx [math/PI] 4))
false
(vec/delta-eq [math/PI] (vec/approx [math/PI] 4) 0.000001)
false
source: notebooks/noj_book/linear_algebra_intro.clj