12  Intro to Linear Algebra - DRAFT ๐Ÿ› 

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

For additional backgrounds, see Introduction to Applied Linear Algebra โ€“ Vectors, Matrices, and Least Squares by Stephen Boyd & Lieven Vandenberghe, Cambridge University Press, UK, 2018.

(ns noj-book.linear-algebra-intro
  (:require [fastmath.vector :as vec]))

util vars

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

12.1 Addition

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

12.2 Scalar multiplication

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

12.3 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]

12.4 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

12.5 Converters

12.5.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

12.5.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

12.5.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

12.5.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

12.5.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]

12.5.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]
source: notebooks/noj_book/linear_algebra_intro.clj