# 12 Intro to Linear Algebra - DRAFT π

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

## 12.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.

## 12.2 Recommended reading

### 12.2.1 Applied

Introduction to Applied Linear Algebra β Vectors, Matrices, and Least Squares by Stephen Boyd & Lieven Vandenberghe, Cambridge University Press, UK, 2018.

Numerical Linear Algebra for Programmers - An Interactive Tutorial with GPU, CUDA, OpenCL, MKL, Java, and Clojure by Dragan Djurich, 2019.

Mathematics for Machine Learning by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon, Cambridge University Press, 2020.

### 12.2.2 Abstract

- Linear Algebra Done Right 4th by Sheldon Axler, Springer, 2024.

## 12.3 Setup

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

util vars

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

## 12.4 What is a linear transformation?

(coming soon)

## 12.5 Addition

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

`1.0 6.0] [`

## 12.6 Scalar multiplication

```
1 9]
(vec/mult [1000)
```

`1000.0 9000.0] [`

## 12.7 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.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`

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

`100.0`

## 12.9 Converters

### 12.9.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.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`

### 12.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`

### 12.9.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.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??

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

`5 10] [`

### 12.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`

.

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

`0.0 0.0 0.0] [`

Magnitude of the vector calculated using Pythagoras(norm).

`3 4]) (vec/mag [`

`5.0`

Round the value from the vector based on second argument

` (vec/approx [math/PI])`

`3.14] [`

`5) (vec/approx [math/PI math/PI math/PI] `

`3.14159 3.14159 3.14159] [`

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

`4)) (vec/edelta-eq [math/PI] (vec/approx [math/PI] `

`false`

`4) 0.001) (vec/edelta-eq [math/PI] (vec/approx [math/PI] `

`true`

`4) 0.000001) (vec/edelta-eq [math/PI] (vec/approx [math/PI] `

`false`

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

`4)) (vec/delta-eq [math/PI] (vec/approx [math/PI] `

`false`

`4) 0.000001) (vec/delta-eq [math/PI] (vec/approx [math/PI] `

`false`