Top Banner
Standardising a linear algebra library Guy Davidson Meeting C++ 15/11/2018 @hatcat01 1
241

Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Apr 04, 2019

Download

Documents

phungnga
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Standardising a linear algebra library

Guy DavidsonMeeting C++ 15/11/2018

@hatcat01 1

Page 2: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

What to expect...0. Representing linear equations [10-68]

1. I can do better than this [70-108]

2. Everything you need to know about storage [110-120]

3. The upsetting story of std::complex [122-191]

4. Alternative algorithms [193-211]

5. Assembling the API [213-238]

@hatcat01 2

Page 3: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

But first, our goalsProvide linear algebra vocabulary types

3

Page 4: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

But first, our goalsProvide linear algebra vocabulary types

Parameterise orthogonal aspects of implementation

4

Page 5: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

But first, our goalsProvide linear algebra vocabulary types

Parameterise orthogonal aspects of implementation

Defaults for the 90%, customisable for power users

5

Page 6: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

But first, our goalsProvide linear algebra vocabulary types

Parameterise orthogonal aspects of implementation

Defaults for the 90%, customisable for power users

Element access, matrix arithmetic, fundamental operations

6

Page 7: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

But first, our goalsProvide linear algebra vocabulary types

Parameterise orthogonal aspects of implementation

Defaults for the 90%, customisable for power users

Element access, matrix arithmetic, fundamental operations

Solve common least-squares and eigenvalue problems

7

Page 8: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

But first, our goalsProvide linear algebra vocabulary types

Parameterise orthogonal aspects of implementation

Defaults for the 90%, customisable for power users

Element access, matrix arithmetic, fundamental operations

Solve common least-squares and eigenvalue problems

Mixed precision and mixed representation expressions

8

Page 9: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

What to expect...0. Representing linear equations

1. I can do better than this

2. Everything you need to know about storage

3. The upsetting story of std::complex

4. Alternative algorithms

5. Assembling the API

@hatcat01 9

Page 10: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101“The branch of mathematics concerning linear equations and linear functions, and their representation through matrices and vector spaces”

@hatcat01 10

Page 11: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101“The branch of mathematics concerning linear equations and linear functions, and their representation through matrices and vector spaces”

a1x1 + a2x2 + … + anxn = b

@hatcat01 11

Page 12: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101“The branch of mathematics concerning linear equations and linear functions, and their representation through matrices and vector spaces”

a1x1 + a2x2 + … + anxn = b

Simultaneous equations

@hatcat01 12

Page 13: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101“The branch of mathematics concerning linear equations and linear functions, and their representation through matrices and vector spaces”

a1x1 + a2x2 + … + anxn = b

Simultaneous equations

Geometry

@hatcat01 13

Page 14: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101“The branch of mathematics concerning linear equations and linear functions, and their representation through matrices and vector spaces”

a1x1 + a2x2 + … + anxn = b

Simultaneous equations

Geometry

Linear regression

@hatcat01 14

Page 15: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101(a1, a2 … an)

@hatcat01 15

Page 16: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101(a1, a2 … an)

(a1, a2 … an) + (b1, b2 … bn) = (a1+b1, a2+b2 … an+bn)

@hatcat01 16

Page 17: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101(a1, a2 … an)

(a1, a2 … an) + (b1, b2 … bn) = (a1+b1, a2+b2 … an+bn)

b * (a1, a2 … an) = (ba1, ba2 … ban)

@hatcat01 17

Page 18: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101(a1, a2 … an)

(a1, a2 … an) + (b1, b2 … bn) = (a1+b1, a2+b2 … an+bn)

b * (a1, a2 … an) = (ba1, ba2 … ban)

(b1)(a1, a2, a3) . (b2) = a1b1 + a2b2 + a3b3 (b3)

@hatcat01 18

Page 19: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101(a11, … a1n)(a21, … a2n)(a31, … a3n)

@hatcat01 19

Page 20: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101(a11, … a1n)(a21, … a2n)(a31, … a3n)

(a11, … a1n) (b11, … b1n) (a11+b11, … a1n+b1n)(a21, … a2n) + (b21, … b2n) = (a21+b21, … a2n+b2n)(a31, … a3n) (b31, … b3n) (a31+b31, … a3n+b3n)

@hatcat01 20

Page 21: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101 (a11, … a1n) (ba11, … ba1n)b * (a21, … a2n) = (ba21, … ba2n) (a31, … a3n) (ba31, … ba3n)

@hatcat01 21

Page 22: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101 (a11, … a1n) (ba11, … ba1n)b * (a21, … a2n) = (ba21, … ba2n) (a31, … a3n) (ba31, … ba3n)

(a11, … a1n) (b11, b12, b13) (a1.b1, a1.b2, a1.b3)(a21, … a2n) * ( … ) = (a2.b1, a2.b2, a2.b3)(a31, … a3n) (bn1, bn2, bn3) (a3.b1, a3.b2, a3.b3)

@hatcat01 22

Page 23: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101 (a11, … a1n) (ba11, … ba1n)b * (a21, … a2n) = (ba21, … ba2n) (a31, … a3n) (ba31, … ba3n)

(a11, … a1n) (b11, b12, b13) (a1.b1, a1.b2, a1.b3)(a21, … a2n) * ( … ) = (a2.b1, a2.b2, a2.b3)(a31, … a3n) (bn1, bn2, bn3) (a3.b1, a3.b2, a3.b3)

A*B != B*A

@hatcat01 23

Page 24: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101 (a11, a12, … a1n)A = (a21, a22, … a2n) (… … … … … … … …) (an1, an2, … ann)

24

Page 25: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101 (a11, a12, … a1n) (1, 0, … 0)A = (a21, a22, … a2n) I = (0, 1, … 0) (… … … … … … … …) (… … … … …) (an1, an2, … ann) (0, 0, … 1)

25

Page 26: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101 (a11, a12, … a1n) (1, 0, … 0)A = (a21, a22, … a2n) I = (0, 1, … 0) (… … … … … … … …) (… … … … …) (an1, an2, … ann) (0, 0, … 1)

Determinant of A = |A|

26

Page 27: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101 (a11, a12, … a1n) (1, 0, … 0)A = (a21, a22, … a2n) I = (0, 1, … 0) (… … … … … … … …) (… … … … …) (an1, an2, … ann) (0, 0, … 1)

Determinant of A = |A|

Inverse of A = A-1

27

Page 28: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101 (a11, a12, … a1n) (1, 0, … 0)A = (a21, a22, … a2n) I = (0, 1, … 0) (… … … … … … … …) (… … … … …) (an1, an2, … ann) (0, 0, … 1)

Determinant of A = |A|

Inverse of A = A-1

A-1*A = A*A-1 = I

28

Page 29: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101operator+(), operator-()

operator*(), operator/()

operator*() overload

operator++(), operator--()

operator<(), operator>()

@hatcat01 29

Page 30: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101ax + by = ecx + dy = f

@hatcat01 30

Page 31: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101ax + by = ecx + dy = f

(a b)*(x) = (e)(c d) (y) (f)

@hatcat01 31

Page 32: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101ax + by = ecx + dy = f

(a b)*(x) = (e)(c d) (y) (f)

A*(x) = (e) (y) (f)

@hatcat01 32

Page 33: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101ax + by = ecx + dy = f

(a b)*(x) = (e)(c d) (y) (f)

A*(x) = (e) (y) (f)

(x) = A-1*(e)(y) (f)

@hatcat01 33

Page 34: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

2x + 3y = 8 x - 2y = -3

Linear algebra 101

@hatcat01 34

Page 35: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

A = (2 3) (1 -2)

2x + 3y = 8 x - 2y = -3

Linear algebra 101

@hatcat01 35

Page 36: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

A = (2 3) (1 -2)

|A|-1 * classical adjoint(A)

2x + 3y = 8 x - 2y = -3

Linear algebra 101

@hatcat01 36

Page 37: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

2x + 3y = 8 x - 2y = -3

A = (2 3) (1 -2)

|A|-1 * classical adjoint(A)

|A| = (2 * -2) - (1 * 3) = -7

Linear algebra 101

@hatcat01 37

Page 38: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

2x + 3y = 8 x - 2y = -3

Linear algebra 101A = (2 3) (1 -2)

|A|-1 * classical adjoint(A)

|A| = (2 * -2) - (1 * 3) = -7

classical adjoint A = (-2 -3) (-1 2)

@hatcat01 38

Page 39: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

2x + 3y = 8 x - 2y = -3

Linear algebra 101A = (2 3) (1 -2)

|A| = -7

classical adjoint A = (-2 -3) (-1 2)

A-1 = -7-1 * (-2 -3) (-1 2)

@hatcat01 39

Page 40: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

2x + 3y = 8 x - 2y = -3

Linear algebra 101A = (2 3) (1 -2)

A-1 = -7-1 * (-2 -3) (-1 2)

@hatcat01 40

Page 41: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

2x + 3y = 8 x - 2y = -3

Linear algebra 101A = (2 3) (1 -2)

A-1 = -7-1 * (-2 -3) (-1 2)

(x) = -7-1 * (-2 -3) * (8)(y) (-1 2) (3)

@hatcat01 41

Page 42: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

2x + 3y = 8 x - 2y = -3

Linear algebra 101A = (2 3) (1 -2)

A-1 = -7-1 * (-2 -3) (-1 2)

(x) = -7-1 * (-2 -3) * (8)(y) (-1 2) (3)

(x) = ((-2 * 8) + (-3 * 3)) / -7(y) ((-1 * 8) + ( 2 * 3)) / -7

@hatcat01 42

Page 43: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

2x + 3y = 8 x - 2y = -3

Linear algebra 101A = (2 3) (1 -2)

A-1 = -7-1 * (-2 -3) (-1 2)

(x) = -7-1 * (-2 -3) * (8)(y) (-1 2) (3)

(x) = (1)(y) (2)

@hatcat01 43

Page 44: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101a1x1 + a2x2 + … + anxn = b

@hatcat01 44

Page 45: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101a1x1 + a2x2+ … + anxn = b

a1x1 + a2x2 = b

@hatcat01 45

Page 46: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101a1x1 + a2x2 + … + anxn = b

a1x1 + a2x2 = b

ax + by = c

@hatcat01 46

Page 47: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101a1x1 + a2x2 + … + anxn = b

a1x1 + a2x2 = b

ax + by = c

by = -ax + c

@hatcat01 47

Page 48: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101a1x1 + a2x2 + … + anxn = b

a1x1 + a2x2 = b

ax + by = c

by = -ax + c

y = mx + c

@hatcat01 48

Page 49: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Translate

(x, y) + (a, b) = (x+a, y+b)

@hatcat01 49

Page 50: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Scale

(x, y) * 2 = (2x, 2y)

(x, y) * (2 0) = (2x, 2y) (0 2)

@hatcat01 50

Page 51: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Shear

(x, y) * (1 4) = (x, 4x + y) (0 1)

@hatcat01 51

Page 52: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Reflect

(x, y) * (-1 0) = (-x, y) ( 0 1)

@hatcat01 52

Page 53: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Rotate

(x, y) * (cos a -sin a) (sin a cos a)

= (x*cos a + y*sin a, -x*sin a + y*cos a)

@hatcat01 53

Page 54: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Reflect and shear

(x, y) * (-1 0) * (1 4) ( 0 1) (0 1)

(x, y) * (-1 -4) ( 0 1)

@hatcat01 54

Page 55: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Linear regression

@hatcat01 55

Page 56: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Linear regression

Given x, what is y?

@hatcat01 56

Page 57: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101Linear regression

Given x, what is y?

Predictive model

@hatcat01 57

Page 58: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101{yi, xi1, xi2, … xip}

ni=1

@hatcat01 58

Page 59: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101{yi, xi1, xi2, … xip}

ni=1

yi = β01 + β1xi1 + ... + βpxip + εi = xiTβ + εi , i = 1,...,n

@hatcat01 59

Page 60: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101{yi, xi1, xi2, … xip}

ni=1

yi = β01 + β1xi1 + ... + βpxip + εi = xiTβ + εi , i = 1,...,n

y = Xβ + ε

@hatcat01 60

Page 61: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101{yi, xi1, xi2, … xip}

ni=1

yi = β01 + β1xi1 + ... + βpxip + εi = xiTβ + εi , i = 1,...,n

y = Xβ + ε

(y1)y = (y2) (… ) (yn)

@hatcat01 61

Page 62: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101{yi, xi1, xi2, … xip}

ni=1

yi = β01 + β1xi1 + ... + βpxip + εi = xiTβ + εi , i = 1,...,n

y = Xβ + ε

(y1) (x1T) (1 x11 … x1p)

y = (y2) X = (x2T) = (1 x21 … x2p)

(… ) ( … ) (… … … … ) (yn) (xn

T) (1 xn1 … xnp)

@hatcat01 62

Page 63: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101{yi, xi1, xi2, … xip}

ni=1

yi = β01 + β1xi1 + ... + βpxip + εi = xiTβ + εi , i = 1,...,n

y = Xβ + ε

(y1) (x1T) (1 x11 … x1p) (β0)

y = (y2) X = (x2T) = (1 x21 … x2p) β = (β1)

(… ) (… ) (… … … … ) (… ) (yn) (xn

T) (1 xn1 … xnp) (βp)

@hatcat01 63

Page 64: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101{yi, xi1, xi2, … xip}

ni=1

yi = β01 + β1xi1 + ... + βpxip + εi = xiTβ + εi , i = 1,...,n

y = Xβ + ε

(y1) (x1T) (1 x11 … x1p) (β0) (ε0)

y = (y2) X = (x2T) = (1 x21 … x2p) β = (β1) ε = (ε1)

(… ) (… ) (… … … … ) (… ) (…) (yn) (xn

T) (1 xn1 … xnp) (βp) (εn)

@hatcat01 64

Page 65: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101

@hatcat01 65

Page 66: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101hi = β1ti + β2ti

2 + εi

@hatcat01 66

Page 67: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101hi = β1ti + β2ti

2 + εi

xi = (xi1, xi2) = (ti, ti2)

@hatcat01 67

Page 68: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Linear algebra 101hi = β1ti + β2ti

2 + εi

xi = (xi1, xi2) = (ti, ti2)

hi = xiTβ + εi

@hatcat01 68

Page 69: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

What to expect...0. Representing linear equations

1. I can do better than this

2. Everything you need to know about storage

3. The upsetting story of std::complex

4. Alternative algorithms

5. Assembling the API

@hatcat01 69

Page 70: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artFixed point, 80286 (no maths coprocessor)

@hatcat01 70

Page 71: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artFixed point, 80286 (no maths coprocessor)

Floating point, 80486

@hatcat01 71

Page 72: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artFixed point, 80286 (no maths coprocessor)

Floating point, 80486

SSE2, Pentium IV

@hatcat01 72

Page 73: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artFixed point, 80286 (no maths coprocessor)

Floating point, 80486

SSE2, Pentium IV

AVX, 2011 (Sandy Bridge?)

@hatcat01 73

Page 74: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artOptimisations available through specialisation

@hatcat01 74

Page 75: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artOptimisations available through specialisation

Matrix size

@hatcat01 75

Page 76: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artOptimisations available through specialisation

Matrix size

float

@hatcat01 76

Page 77: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artOptimisations available through specialisation

Matrix size

float

SIMD instruction set

@hatcat01 77

Page 78: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artOptimisations available through specialisation

Matrix size

float

SIMD instruction set

Cache line size

@hatcat01 78

Page 79: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artOptimisations available through specialisation

Matrix size

float

SIMD instruction set

Cache line size

Dense

@hatcat01 79

Page 80: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artBLAS (Basic Linear Algebra Subprograms)

@hatcat01 80

Page 81: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artBLAS (Basic Linear Algebra Subprograms)

BLAS++

@hatcat01 81

Page 82: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artBLAS (Basic Linear Algebra Subprograms)

BLAS++

void blas::axpy(int64_t n, float alpha, float const* x, int64_t incx, float* y, int64_t incy);

@hatcat01 82

Page 83: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artBLAS (Basic Linear Algebra Subprograms)

BLAS++

void blas::axpy(int64_t n, float alpha, float const* x, int64_t incx, float* y, int64_t incy);

Boost.uBLAS

@hatcat01 83

Page 84: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artasum vector 1 norm (sum)axpy add vectorscopy copy vectordot dot productdotu dot product, unconjugatediamax max elementnrm2 vector 2 normrot apply Givens plane rotationrotg generate Givens plane rotationrotm apply modified Givens plane rotationrotmg generate modified Givens plane rotationscal scale vectorswap swap vectors

@hatcat01 84

Page 85: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artasum gemv general matrix-vector multiplyaxpy ger general matrix rank 1 updatecopy hemv hermitian matrix-vector multiplydot her hermitian rank 1 updatedotu her2 hermitian rank 2 updateiamax symv symmetric matrix-vector multiplynrm2 syr symmetric rank 1 updaterot syr2 symmetric rank 2 updaterotg trmv triangular matrix-vector multiplyrotm trsv triangular matrix-vector solverotmgscalswap

@hatcat01 85

Page 86: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artasum gemv gemm general matrix multiply: C = AB + Caxpy ger hemm hermitian matrix multiplycopy hemv herk hermitian rank k updatedot her her2k hermitian rank 2k updatedotu her2 symm symmetric matrix multiplyiamax symv syrk symmetric rank k updatenrm2 syr syr2k symmetric rank 2k updaterot syr2 trmm triangular matrix multiplyrotg trmv trsm triangular solve matrixrotm trsvrotmgscalswap

@hatcat01 86

Page 87: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artEigen

@hatcat01 87

Page 88: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artEigen

Matrix and vector class templates

@hatcat01 88

Page 89: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artEigen

Matrix and vector class templates

Dynamic or static sizes

@hatcat01 89

Page 90: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artEigen

Matrix and vector class templates

Dynamic or static sizes

Span option via Eigen::Map

@hatcat01 90

Page 91: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeHow many member functions does string have which are NOT special functions?

91

Page 92: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artEigen

Matrix and vector class templates

Dynamic or static sizes

Span option via Eigen::Map

Member function API

@hatcat01 92

Page 93: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior art#include <iostream>#include <Eigen/Dense>using namespace Eigen;using namespace std;

int main() { MatrixXd m = MatrixXd::Random(3,3); m = (m + MatrixXd::Constant(3,3,1.2)) * 50; cout << "m =" << endl << m << endl; VectorXd v(3); v << 1, 2, 3; cout << "m * v =" << endl << m * v << endl;}

@hatcat01 93

Page 94: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artDlib

@hatcat01 94

Page 95: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artDlib

Expression templates

@hatcat01 95

Page 96: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artclass row_vector { public: row_vector(size_t n) : elems(n) {} double &operator[](size_t i) { return elems[i]; } double operator[](size_t i) const { return elems[i]; } size_t size() const { return elems.size(); } private: std::vector<float> elems;};

@hatcat01 96

Page 97: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artrow_vector operator+(row_vector const &u, row_vector const &v) { row_vector sum(u.size()); for (size_t i = 0; i < u.size(); i++) sum[i] = u[i] + v[i]; return sum;}

auto a = row_vector(4);auto b = row_vector(4);auto c = row_vector(4);...auto d = a + b + c;

@hatcat01 97

Page 98: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artDelayed evaluation

@hatcat01 98

Page 99: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artDelayed evaluation

row_vector_sum operator+(...

@hatcat01 99

Page 100: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artDelayed evaluation

row_vector_sum operator+(...

Expression trees

@hatcat01 100

Page 101: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artDelayed evaluation

row_vector_sum operator+(...

Expression trees

Compile time evaluation

@hatcat01 101

Page 102: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior arttemplate <typename E>class vector_expression { public: double operator[](size_t i) const { return static_cast<E const&>(*this)[i]; } size_t size() const { return static_cast<E const&>(*this).size(); }};

@hatcat01 102

Page 103: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior artrow_vector(std::initializer_list<float>init) { for (auto i:init) elems.push_back(i);}

template <typename E>row_vector(vector_expression<E> const& vec) : elems(vec.size()) { for (size_t i = 0; i != vec.size(); ++i) elems[i] = vec[i];}

@hatcat01 103

Page 104: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior arttemplate <typename E1, typename E2>class vector_sum : public vector_expression<vector_sum<E1, E2>> {public: vector_sum(E1 const& u, E2 const& v) : _u(u), _v(v) {} double operator[](size_t i) const { return _u[i] + _v[i]; } size_t size() const { return _v.size(); }private: E1 const& _u; E2 const& _v;};

@hatcat01 104

Page 105: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior arttemplate <typename E1, typename E2>vector_sum<E1,E2> operator+(E1 const& u, E2 const& v) { return vector_sum<E1, E2>(u, v); }

@hatcat01 105

Page 106: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior arttemplate <typename E1, typename E2>vector_sum<E1,E2> operator+(E1 const& u, E2 const& v) { return vector_sum<E1, E2>(u, v); }

vector_sum<vector_sum<row_vector, row_vector>, row_vector> d = a + b + c;

@hatcat01 106

Page 107: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior arttemplate <typename E1, typename E2>vector_sum<E1,E2> operator+(E1 const& u, E2 const& v) { return vector_sum<E1, E2>(u, v); }

vector_sum<vector_sum<row_vector, row_vector>, row_vector> d = a + b + c;

elems[i] = vec[i];

@hatcat01 107

Page 108: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Prior arttemplate <typename E1, typename E2>vector_sum<E1,E2> operator+(E1 const& u, E2 const& v) { return vector_sum<E1, E2>(u, v); }

vector_sum<vector_sum<row_vector, row_vector>, row_vector> d = a + b + c;

elems[i] = vec[i];

elems[i] = a.elems[i] + b.elems[i] + c.elems[i];

@hatcat01 108

Page 109: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

What to expect...0. Representing linear equations

1. I can do better than this

2. Everything you need to know about storage

3. The upsetting story of std::complex

4. Alternative algorithms

5. Assembling the API

@hatcat01 109

Page 110: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

StorageFixed size

@hatcat01 110

Page 111: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

StorageFixed size

Sparse

@hatcat01 111

Page 112: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

StorageFixed size

Sparse

Dynamic size

@hatcat01 112

Page 113: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

StorageFixed size

Sparse

Dynamic size

View

@hatcat01 113

Page 114: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

StorageCache lines

@hatcat01 114

Page 115: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

StorageCache lines

SIMD

@hatcat01 115

Page 116: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

StorageCache lines

SIMD

Paramaterise

@hatcat01 116

Page 117: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Storagetemplate <typename scalar, size_t row_count, size_t column_count>class fixed_size_matrix{ public: constexpr fixed_size_matrix() noexcept; constexpr fixed_size_matrix(std::initializer_list<scalar> &&) noexcept; constexpr scalar& operator()(size_t, size_t); constexpr scalar operator()(size_t, size_t) const; private: scalar e[row_count * col_count];};

operator[](std::pair<size_t, size_t>); // To be implemented

@hatcat01 117

Page 118: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Storagetemplate<typename mdspan>class matrix_view{ public: using scalar = mdspan::element_type; constexpr matrix_view(mdspan) noexcept; constexpr scalar operator()(size_t, size_t) const; constexpr size_t columns() const noexcept; constexpr size_t rows() const noexcept; private: mdspan m_span;};

@hatcat01 118

Page 119: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Storagetemplate <typename scalar, typename allocator>class dynamic_size_matrix{ public: constexpr dynamic_size_matrix() noexcept; constexpr dynamic_size_matrix(std::initializer_list<scalar> &&) noexcept; constexpr scalar& operator()(size_t, size_t); constexpr scalar operator()(size_t, size_t) const; constexpr size_t columns() const noexcept; constexpr size_t rows() const noexcept;

@hatcat01 119

Page 120: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Storage constexpr size_t column_capacity() const noexcept; constexpr size_t row_capacity() const noexcept; void reserve (size_t, size_t); void resize (size_t, size_t);

private: unique_ptr<scalar> e; size_t m_rows; size_t m_cols; size_t m_row_capacity; size_t m_column_capacity;};

@hatcat01 120

Page 121: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

What to expect...0. Representing linear equations

1. I can do better than this

2. Everything you need to know about storage

3. The upsetting story of std::complex

4. Alternative algorithms

5. Assembling the API

@hatcat01 121

Page 122: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3;

@hatcat01 122

Page 123: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3; // int a = 11

@hatcat01 123

Page 124: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3; // int a = 11

auto a = 7 * 5 / 3l;

@hatcat01 124

Page 125: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3; // int a = 11

auto a = 7 * 5 / 3l; // long a = 11l

@hatcat01 125

Page 126: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3; // int a = 11

auto a = 7 * 5 / 3l; // long a = 11l

auto a = 7 * 5 / -3ul;

@hatcat01 126

Page 127: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3; // int a = 11

auto a = 7 * 5 / 3l; // long a = 11l

auto a = 7 * 5 / -3ul; // unsigned long a = 0ul

@hatcat01 127

Page 128: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3; // int a = 11

auto a = 7 * 5 / 3l; // long a = 11l

auto a = 7 * 5 / -3ul; // unsigned long a = 0ul

long a = 7 * 5 / -3ul;

@hatcat01 128

Page 129: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3; // int a = 11

auto a = 7 * 5 / 3l; // long a = 11l

auto a = 7 * 5 / -3ul; // unsigned long a = 0ul

long a = 7 * 5 / -3ul; // long a = 0l

@hatcat01 129

Page 130: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.;

@hatcat01 130

Page 131: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

@hatcat01 131

Page 132: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

auto a = 7. * 5.f / 3;

@hatcat01 132

Page 133: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

auto a = 7. * 5.f / 3; // double a = 11.666666666666666

@hatcat01 133

Page 134: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

auto a = 7. * 5.f / 3; // double a = 11.666666666666666

auto a = 7.f * 5.f / 3;

@hatcat01 134

Page 135: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

auto a = 7. * 5.f / 3; // double a = 11.666666666666666

auto a = 7.f * 5.f / 3; // float a = 11.666667f

@hatcat01 135

Page 136: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

auto a = 7. * 5.f / 3; // double a = 11.666666666666666

auto a = 7.f * 5.f / 3; // float a = 11.666667f

auto a = 7.f * 5.f / -3l;

@hatcat01 136

Page 137: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

auto a = 7. * 5.f / 3; // double a = 11.666666666666666

auto a = 7.f * 5.f / 3; // float a = 11.666667f

auto a = 7.f * 5.f / -3l; // float a = -11.666667f

@hatcat01 137

Page 138: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

auto a = 7. * 5.f / 3; // double a = 11.666666666666666

auto a = 7.f * 5.f / 3; // float a = 11.666667f

auto a = 7.f * 5.f / -3l; // float a = -11.666667f

auto a = 7.f * 5.f / -3ul;

@hatcat01 138

Page 139: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = 7 * 5 / 3.; // double a = 11.666666666666666

auto a = 7. * 5.f / 3; // double a = 11.666666666666666

auto a = 7.f * 5.f / 3; // float a = 11.666667f

auto a = 7.f * 5.f / -3l; // float a = -11.666667f

auto a = 7.f * 5.f / -3ul; // float a = // 0.0000000000000000018973538f

@hatcat01 139

Page 140: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionIntegral promotion

@hatcat01 140

Page 141: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionIntegral promotion

Floating point promotion

@hatcat01 141

Page 142: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionIntegral promotion

Floating point promotion

Integral conversions

@hatcat01 142

Page 143: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionIntegral promotion

Floating point promotion

Integral conversions

Floating-point conversions

@hatcat01 143

Page 144: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionIntegral promotion

Floating point promotion

Integral conversions

Floating-point conversions

Floating-integral conversions

@hatcat01 144

Page 145: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionIntegral promotion

Floating point promotion

Integral conversions

Floating-point conversions

Floating-integral conversions

(Search for integral promotion at cppreference.com)

@hatcat01 145

Page 146: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionPromotion:

float->double, int->long, widening representation

@hatcat01 146

Page 147: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionPromotion:

float->double, int->long, widening representation

Conversion:

integral->floating point, changing representation

@hatcat01 147

Page 148: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionPromotion:

float->double, int->long, widening representation

Conversion:

integral->floating point, changing representation

ftol()

@hatcat01 148

Page 149: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionPromotion:

float->double, int->long, widening representation

Conversion:

integral->floating point, changing representation

ftol()

int a = b * 3.5;

@hatcat01 149

Page 150: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversion

@hatcat01

(3 5 5) (1.0 3.3 6.8) (4.0 8.3 11.8)(4 4 3) + (3.0 2.5 7.3) = (7.0 6.5 10.3)(1 0 1) (2.1 4.8 4.4) (3.1 4.8 5.4)

150

Page 151: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversion

@hatcat01

(3 5 5) (1.0 3.3 6.8) (4.0 8.3 11.8)(4 4 3) + (3.0 2.5 7.3) = (7.0 6.5 10.3)(1 0 1) (2.1 4.8 4.4) (3.1 4.8 5.4)

template<class T1, class T2> using element_promotion_t =typename element_promotion<T1, T2>::type;

151

Page 152: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T> struct is_complex : public false_type {};

@hatcat01 152

Page 153: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T> struct is_complex : public false_type {};

template<class T> struct is_complex<std::complex<T>> : public std::bool_constant<std::is_arithmetic_v<T>> {};

@hatcat01 153

Page 154: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T> struct is_complex : public false_type {};

template<class T> struct is_complex<std::complex<T>> : public std::bool_constant<std::is_arithmetic_v<T>> {};

template<class T>inline constexpr bool is_complex_v = is_complex<T>::value;

@hatcat01 154

Page 155: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T> struct is_matrix_element : public std::bool_constant<std::is_arithmetic_v<T> || is_complex_v<T>> {};

@hatcat01 155

Page 156: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T> struct is_matrix_element : public std::bool_constant<std::is_arithmetic_v<T> || is_complex_v<T>> {};

template<class T>inline constexpr bool is_matrix_element_v = is_matrix_element<T>::value;

@hatcat01 156

Page 157: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T1, class T2>struct element_promotion_helper { static_assert(std::is_arithmetic_v<T1> && std::is_arithmetic_v<T2>); using type = decltype(T1() * T2());};

@hatcat01 157

Page 158: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T1, class T2>struct element_promotion_helper { static_assert(std::is_arithmetic_v<T1> && std::is_arithmetic_v<T2>); using type = decltype(T1() * T2());};

template<class T1, class T2>using element_promotion_helper_t = typename element_promotion_helper<T1, T2>::type;

@hatcat01 158

Page 159: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T1, class T2>struct element_promotion_helper { static_assert(std::is_arithmetic_v<T1> && std::is_arithmetic_v<T2>); using type = decltype(T1() * T2());};

template<class T1, class T2>using element_promotion_helper_t = typename element_promotion_helper<T1, T2>::type;

template<class T1, class T2>struct element_promotion { using type = element_promotion_helper_t<T1, T2>;};

@hatcat01 159

Page 160: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T1, class T2>struct element_promotion<T1, std::complex<T2>> { static_assert(std::is_same_v<T1, T2>); using type = std::complex<element_promotion_helper_t<T1, T2>>;};

@hatcat01 160

Page 161: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T1, class T2>struct element_promotion<T1, std::complex<T2>> { static_assert(std::is_same_v<T1, T2>); using type = std::complex<element_promotion_helper_t<T1, T2>>;};

template<class T1, class T2>struct element_promotion<std::complex<T1>, T2> { static_assert(std::is_same_v<T1, T2>); using type = std::complex<element_promotion_helper_t<T1, T2>>;};

@hatcat01 161

Page 162: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T1, class T2>struct element_promotion<std::complex<T1>, std::complex<T2>> { static_assert(std::is_same_v<T1, T2>); using type = std::complex<element_promotion_helper_t<T1, T2>>;};

@hatcat01 162

Page 163: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversiontemplate<class T1, class T2>struct element_promotion<std::complex<T1>, std::complex<T2>> { static_assert(std::is_same_v<T1, T2>); using type = std::complex<element_promotion_helper_t<T1, T2>>;};

template<class T1, class T2>using element_promotion_t = typename element_promotion<T1, T2>::type;

@hatcat01 163

Page 164: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<int>(7, 0) * complex<int>(5, 0) / complex<int>(3, 0);

@hatcat01 164

Page 165: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<int>(7, 0) * complex<int>(5, 0) / complex<int>(3, 0);// complex<int> a = {17,0}

@hatcat01 165

Page 166: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<int>(7, 0) * complex<int>(5, 0) / complex<int>(3, 0);// complex<int> a = {17,0}

auto a = complex<int>(7.0, 0.0) * complex<int>(5, 0) / complex<int>(3.0, 0.0);

@hatcat01 166

Page 167: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<int>(7, 0) * complex<int>(5, 0) / complex<int>(3, 0);// complex<int> a = {17,0}

auto a = complex<int>(7.0, 0.0) * complex<int>(5, 0) / complex<int>(3.0, 0.0);// complex<int> a = {17,0}

@hatcat01 167

Page 168: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<int>(7, 0) * complex<int>(5, 0) / complex<int>(3, 0);// complex<int> a = {17,0}

auto a = complex<int>(7.0, 0.0) * complex<int>(5, 0) / complex<int>(3.0, 0.0);// complex<int> a = {17,0}

auto a = complex<float>(7.0, 0.0) * complex<float>(5, 0) / complex<float>(3.0, 0.0);

@hatcat01 168

Page 169: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<int>(7, 0) * complex<int>(5, 0) / complex<int>(3, 0);// complex<int> a = {17,0}

auto a = complex<int>(7.0, 0.0) * complex<int>(5, 0) / complex<int>(3.0, 0.0);// complex<int> a = {17,0}

auto a = complex<float>(7.0, 0.0) * complex<float>(5, 0) / complex<float>(3.0, 0.0);// complex<float> a = {11.6666667f, 0.0f}

@hatcat01 169

Page 170: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<float>(7.0, 0.0) * complex<int>(5, 0) / complex<float>(3.0, 0.0);

@hatcat01 170

Page 171: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<float>(7.0, 0.0) * complex<int>(5, 0) / complex<float>(3.0, 0.0);// malformed

@hatcat01 171

Page 172: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<float>(7.0, 0.0) * complex<int>(5, 0) / complex<float>(3.0, 0.0);// malformed

auto a = complex<float>(7.0f, 0.0f) * complex<double>(5.0, 0.0) / complex<float>(3.0f, 0.0f);

@hatcat01 172

Page 173: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Quiz timeauto a = complex<float>(7.0, 0.0) * complex<int>(5, 0) / complex<float>(3.0, 0.0);// malformed

auto a = complex<float>(7.0f, 0.0f) * complex<double>(5.0, 0.0) / complex<float>(3.0f, 0.0f);// malformed

@hatcat01 173

Page 174: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs;

@hatcat01 174

Page 175: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fs

@hatcat01 175

Page 176: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv;

@hatcat01 176

Page 177: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fs

@hatcat01 177

Page 178: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds;

@hatcat01 178

Page 179: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // ds

@hatcat01 179

Page 180: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs;

@hatcat01 180

Page 181: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fs

@hatcat01 181

Page 182: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv;

@hatcat01 182

Page 183: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fs

@hatcat01 183

Page 184: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fsauto f = mv * ds;

@hatcat01 184

Page 185: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fsauto f = mv * ds; // ds

@hatcat01 185

Page 186: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fsauto f = mv * ds; // dsauto g = ds * fs;

@hatcat01 186

Page 187: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fsauto f = mv * ds; // dsauto g = ds * fs; // ds

@hatcat01 187

Page 188: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fsauto f = mv * ds; // dsauto g = ds * fs; // dsauto h = ds * mv;

@hatcat01 188

Page 189: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fsauto f = mv * ds; // dsauto g = ds * fs; // dsauto h = ds * mv; // ds

@hatcat01 189

Page 190: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fsauto f = mv * ds; // dsauto g = ds * fs; // dsauto h = ds * mv; // dsauto i = ds * ds;

@hatcat01 190

Page 191: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Promotion and conversionauto fs = matrix<fixed_size_matrix<float, 3, 3>>{};auto mv = matrix<matrix_view<float, 3, 3>>{mdspan(blah)};auto ds = matrix<dynamic_size_matrix<float>>{};

auto a = fs * fs; // fsauto b = fs * mv; // fsauto c = fs * ds; // dsauto d = mv * fs; // fsauto e = mv * mv; // fsauto f = mv * ds; // dsauto g = ds * fs; // dsauto h = ds * mv; // dsauto i = ds * ds; // ds

@hatcat01 191

Page 192: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

What to expect...0. Representing linear equations

1. I can do better than this

2. Everything you need to know about storage

3. The upsetting story of std::complex

4. Alternative algorithms

5. Assembling the API

@hatcat01 192

Page 193: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operations(2 2) * (1 4) = ((2*1)+(2*2) (2*4)+(2*1)) = ( 6 10)(3 4) (2 1) ((3*1)+(4*2) (3*4)+(4*1)) (11 16)

@hatcat01 193

Page 194: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operations(2 2) * (1 4) = ((2*1)+(2*2) (2*4)+(2*1)) = ( 6 10)(3 4) (2 1) ((3*1)+(4*2) (3*4)+(4*1)) (11 16)

(2 2) * (0 4) = (0 (2*4)+(2*1)) = (0 10)(3 4) (0 1) (0 (3*4)+(4*1)) (0 16)

@hatcat01 194

Page 195: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsscalar_t inner_product(matrix_t const& lhs, matrix_t const& rhs) { return scalar_t(std::inner_product(lhs.cbegin(), lhs.cend(), rhs.cbegin(), scalar_t(0));}

@hatcat01 195

Page 196: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsscalar_t modulus_squared(matrix_t const& mat) { return std::accumulate(mat.cbegin(), mat.cend(), scalar_t(0), [&](scalar_t tot, const auto& el) { return tot + (el * el); });}

@hatcat01 196

Page 197: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsscalar_t modulus_squared(matrix_t const& mat) { return std::accumulate(mat.cbegin(), mat.cend(), scalar_t(0), [&](scalar_t tot, const auto& el) { return tot + (el * el); });}

scalar_t modulus(matrix_t const& mat) { return std::sqrt(modulus_squared(mat));}

@hatcat01 197

Page 198: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsmatrix_t unit(matrix_t const& mat) { auto res(mat); auto mod(modulus(mat)); std::transform(mat.cbegin(), mat.cend(), res.begin(), [&](const auto& el) { return el / mod; }); return res;}

@hatcat01 198

Page 199: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsmatrix_t transpose(matrix_t const& mat) { auto res = matrix_t{}; for (auto i = 0; i < mat::row(); ++i) { for (auto j = 0; j < mat::col(); ++j) { res._Data[i + j * mat::row()] = mat._Data[i * mat::col() + j]; } } return res;}

@hatcat01 199

Page 200: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operations (1 2 3)M = (4 5 6) (7 8 9)

submatrix(1,1) of M = (5 6) (8 9)

@hatcat01 200

Page 201: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsauto submatrix(matrix_t const& mat, size_t i, size_t j) { auto l_in = mat.cbegin(); auto res = submatrix_t::matrix_t; auto r_out = res.begin(); for (auto r = 0U; r < mat.row(); ++r) { for (auto c = 0U; c < mat.col(); ++c) { if (r != i && c != j) *r_out = *l_in; } ++l_in; } } return res;}

@hatcat01 201

Page 202: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsmatrix_t inverse(matrix_t const& mat);

bool is_invertible(matrix_t const& mat);

@hatcat01 202

Page 203: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsmatrix_t identity(size_t i) { auto res = matrix_t{}; auto out = res.begin(); auto x = res.row() + 1; for (auto y = 0; y != res.row() * res.row(); ++y, ++out) { *out = (x == res.row() + 1 ? 1 : 0; if (--x == 0) x = res.row() + 1; } return res;}

@hatcat01 203

Page 204: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsscalar_t determinant(matrix_t const& mat) { if constexpr (mat::row() == 1) return mat._Data[0]; else if constexpr (mat::row() == 2) return (mat._Data[0] * mat._Data[3]) - (mat._Data[1] * mat._Data[2]); else if constexpr (mat::row() > 2) { auto det = scalar_t{0}; auto sign = scalar_t{1}; for (auto f = 0; f < mat::row(); ++f) { auto cofactor = sign * mat._Data[f] * determinant(submatrix(mat, 0, f)); det += cofactor; sign = -sign; } return det;}}

@hatcat01 204

Page 205: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationsmatrix_t classical_adjoint(matrix_t const& mat) { auto res = matrix_t{}; for (auto i = 0; i < mat::row(); ++i) { auto sign = i % 2 == 0 ? scalar_t{1} : scalar_t{-1}; for (auto j = 0; j < mat::row(); ++j) { auto det = determinant(submatrix(mat, i, j)); res._Data[i * mat::row() + j] = sign * det; sign = -sign; } } return transpose(res);}

@hatcat01 205

Page 206: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationstemplate <typename Storage>struct matrix_ops { using scalar_t = Storage::scalar_t; using matrix_t = Storage::matrix_t; template <class Ops2> using multiply_t = matrix_ops< typename Storage::template multiply_t<typename Ops2::matrix_t>>;

static constexpr bool equal(matrix_t const& lhs, matrix_t const& rhs) noexcept; ... template <typename Ops2> static constexpr typename multiply_t<Ops2>::matrix_t matrix_multiply( matrix_t const& lhs, typename Ops2::matrix_t const& rhs) noexcept; ...};

@hatcat01 206

Page 207: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

OperationsMultiplication

@hatcat01 207

Page 208: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

OperationsMultiplication

O(n3)

@hatcat01 208

Page 209: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

OperationsMultiplication

O(n3)

Strassen - O(n2.807)

@hatcat01 209

Page 210: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

OperationsMultiplication

O(n3)

Strassen - O(n2.807)

Best result - O(n2.3728639)

@hatcat01 210

Page 211: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Operationstemplate <typename Storage>struct my_matrix_ops { using scalar_t = Storage::scalar_t; using matrix_t = Storage::matrix_t; template <class Ops2> using multiply_t = matrix_ops< typename Storage::template multiply_t<typename Ops2::matrix_t>>;

static constexpr bool equal(matrix_t const& lhs, matrix_t const& rhs) noexcept { return matrix_ops::equal(lhs, rhs); ... template <typename Ops2> static constexpr typename multiply_t<Ops2>::matrix_t matrix_multiply( matrix_t const& lhs, typename ops2::matrix_t const& rhs) noexcept; ...};

@hatcat01 211

Page 212: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

What to expect...0. Representing linear equations

1. I can do better than this

2. Everything you need to know about storage

3. The upsetting story of std::complex

4. Alternative algorithms

5. Assembling the API

@hatcat01 212

Page 213: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixfixed_size_matrix<float, 3, 3>

@hatcat01 213

Page 214: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixfixed_size_matrix<float, 3, 3>

matrix_ops<fixed_size_matrix<float, 3, 3>>

@hatcat01 214

Page 215: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixfixed_size_matrix<float, 3, 3>

matrix_ops<fixed_size_matrix<float, 3, 3>>

template <typename REP> class matrix;

@hatcat01 215

Page 216: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixfixed_size_matrix<float, 3, 3>

matrix_ops<fixed_size_matrix<float, 3, 3>>

template <typename REP> class matrix;

template <typename REP> class row_vector;

@hatcat01 216

Page 217: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixfixed_size_matrix<float, 3, 3>

matrix_ops<fixed_size_matrix<float, 3, 3>>

template <typename REP> class matrix;

template <typename REP> class row_vector;

template <typename REP> class column_vector;

@hatcat01 217

Page 218: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <typename REP> struct matrix { using scalar_t = typename REP::scalar_t; using matrix_t = typename REP::matrix_t;

constexpr matrix() noexcept = default; constexpr matrix(matrix_t const&) noexcept = default; constexpr matrix(std::initializer_list<scalar_t>) noexcept; constexpr matrix(std::pair<size_t, size_t>) noexcept;

constexpr matrix_t const& data() const noexcept; constexpr matrix_t& data() noexcept; constexpr scalar_t operator()(size_t, size_t) const; constexpr scalar_t& operator()(size_t, size_t);

@hatcat01 218

Page 219: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrix constexpr bool operator==(matrix<REP> const& rhs) const noexcept; constexpr bool operator!=(matrix<REP> const& rhs) const noexcept;

constexpr matrix<REP>& operator*=(scalar_t const& rhs) noexcept; constexpr matrix<REP>& operator/=(scalar_t const& rhs) noexcept;

constexpr matrix<REP>& operator+=(matrix<REP> const& rhs) noexcept; constexpr matrix<REP>& operator-=(matrix<REP> const& rhs) noexcept;

matrix_t _Data;};

@hatcat01 219

Page 220: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <typename REP> constexpr matrix<REP> operator*( matrix<REP> const&, typename matrix<REP>::scalar_t const&) noexcept;

template <typename REP> constexpr matrix<REP> operator*( typename matrix<REP>::scalar_t const&, matrix<REP> const&) noexcept;

template <typename REP> constexpr matrix<REP> operator/( matrix<REP> const&, typename matrix<REP>::scalar_t const&) noexcept;

@hatcat01 220

Page 221: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <typename REP> constexpr auto transpose( matrix<REP> const&) noexcept;

template <typename REP> constexpr auto submatrix( matrix<REP> const&, size_t p, size_t q) noexcept;

template <typename REP> constexpr bool is_invertible( matrix<REP> const&) noexcept;

template <typename REP> constexpr bool is_identity( matrix<REP> const&) noexcept;

@hatcat01 221

Page 222: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <typename REP> constexpr matrix<REP> operator+( matrix<REP> const&, matrix<REP> const&) noexcept;

template <typename REP> constexpr matrix<REP> operator-( matrix<REP> const&, matrix<REP> const&) noexcept;

template <typename REP1, typename REP2> constexpr auto operator*( matrix<REP1> const&, matrix<REP2> const&) noexcept;

@hatcat01 222

Page 223: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <typename REP> constexpr matrix<REP> identity() noexcept;

template <typename REP> constexpr typename REP::scalar_t determinant( matrix<REP> const&);

template <typename REP> constexpr auto classical_adjoint( matrix<REP> const&);

template <typename REP> constexpr matrix<REP> inverse( matrix<REP> const&);

@hatcat01 223

Page 224: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <typename REP> constexpr typename REP::scalar_t inner_product( row_vector<REP> const&, column_vector<REP> const&) noexcept;

template <typename REP> constexpr typename REP::scalar_t modulus( row_vector<REP> const&) noexcept;

template <typename REP> constexpr typename REP::scalar_t modulus_squared( row_vector<REP> const&) noexcept;

template <typename REP> constexpr row_vector<REP> unit( row_vector<REP> const&) noexcept;

@hatcat01 224

Page 225: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixauto f_33 = matrix<matrix_ops<fixed_size_matrix<float, 3, 3>>>{};

auto f_13 = row_vector<matrix_ops<fixed_size_matrix<float, 1, 3>>>{};

auto f_31 = column_vector<matrix_ops<fixed_size_matrix<float, 3, 1>>>{};

@hatcat01 225

Page 226: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrix

@hatcat01 226

Page 227: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixmatrix

@hatcat01 227

Page 228: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixmatrix

row_vector

@hatcat01 228

Page 229: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixmatrix

row_vector

column_vector

@hatcat01 229

Page 230: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixmatrix

row_vector

column_vector

matrix_ops

@hatcat01 230

Page 231: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixmatrix

row_vector

column_vector

matrix_ops

fixed_size_matrix

@hatcat01 231

Page 232: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixmatrix

row_vector

column_vector

matrix_ops

fixed_size_matrix

dynamic_size_matrix

@hatcat01 232

Page 233: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixmatrix

row_vector

column_vector

matrix_ops

fixed_size_matrix

dynamic_size_matrix

matrix_view@hatcat01 233

Page 234: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixauto f_33 = matrix<matrix_ops<fixed_size_matrix<float, 3, 3>>>{};

auto f_13 = row_vector<matrix_ops<fixed_size_matrix<float, 1, 3>>>{};

auto f_31 = column_vector<matrix_ops<fixed_size_matrix<float, 3, 1>>>{};

@hatcat01 234

Page 235: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <size_t M, size_t N>using matrix_impl = std::matrix_ops< std::fixed_size_matrix< float, M, N>>;

@hatcat01 235

Page 236: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <size_t M, size_t N>using matrix_impl = std::matrix_ops< std::fixed_size_matrix< float, M, N>>;

auto m = std::matrix<matrix_impl<3, 3>>{};

@hatcat01 236

Page 237: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <size_t M, size_t N>using matrix_impl = std::matrix_ops< std::fixed_size_matrix< float, M, N>>;

auto m = std::matrix<matrix_impl<3, 3>>{};

using float_33 = std::matrix< std::matrix_ops< std::fixed_size_matrix< float, 3, 3>>>;

@hatcat01 237

Page 238: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Enter The Matrixtemplate <size_t M, size_t N>using matrix_impl = std::matrix_ops< std::fixed_size_matrix< float, M, N>>;

auto m = std::matrix<matrix_impl<3, 3>>{};

using float_33 = std::matrix< std::matrix_ops< std::fixed_size_matrix< float, 3, 3>>>;

auto m = float_33{};

@hatcat01 238

Page 239: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

In summary...0. Representing linear equations

1. I can do better than this

2. Everything you need to know about storage

3. The upsetting story of std::complex

4. Alternative algorithms

5. Assembling the API

https://groups.google.com/a/isocpp.org/forum/#!forum/sg14@hatcat01 239

Page 240: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

A reminder: our goalsProvide linear algebra vocabulary types

Parameterise orthogonal aspects of implementation

Defaults for the 90%, customisable for power users

Element access, matrix arithmetic, fundamental operations

Solve common least-squares and eigenvalue problems

Mixed precision and mixed representation expressions

240

Page 241: Standardising a linear algebra library - meetingcpp.com a linear... · But first, our goals Provide linear algebra vocabulary types Parameterise orthogonal aspects of implementation

Thank You!Ask me two questions...

@hatcat01 241