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
1 9]
(vec/add [0 -3]) [
1.0 6.0] [
12.2 Scalar multiplication
1 9]
(vec/mult [1000)
1000.0 9000.0] [
12.3 Subtraction
When we pass single vector to fastmath/sub function it will be multiplied by -1.0
.
10 5]) (vec/sub [
10.0 -5.0] [-
When we pass two vectors to fastmath/sub function it will perform basic vector subtraction.
10 5]
(vec/sub [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
10 5]
(vec/dot [8 4]) [
100.0
12.5 Converters
12.5.1 Vector to Java array of doubles [D
.
10 5]) (vec/vec->array [
10.0, 5.0] [
type (vec/vec->array [10 5])) (
1 double/
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
.
10 5]) (vec/vec->Vec [
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??
10 2] [5 10 15]) (vec/as-vec [
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
.
5 10 15]) (vec/as-vec [
0.0 0.0 0.0] [