Linear algebra

Solving System of Linear Equations

Solve the following system of 2 linear equations in 2 unknowns:
x - 2y = -5
2x - y = -1
% nap "A = {
{1 -2}
{2 -1}
}"
::NAP::51972-9971
% $A
 1 -2
 2 -1
% nap "B = {-5 -1}"
::NAP::51975-9977
% $B
-5 -1
% nap "x = solve_linear(A, B)"
::NAP::51979-9257
% $x a
::NAP::51979-9257  f64  MissingValue: NaN  References: 1  Unit: (NULL)
Dimension 0   Size: 2      Name: (NULL)    Coordinate-variable: (NULL)
Value:
1 3

Inner-product operator ('.')

The inner-product operator ('.') gives the matrix product if the operands are matrices. Thus we can use it as follows to check the above result:
% [nap "A . x"]
-5 -1
It gives the dot (scalar) product if the operands are vectors. Here are some examples:
% [nap "w = {1r4 1r2 1r4}"]
0.25 0.5 0.25
% [nap "x = {3 2 4}"]
3 2 4
% [nap "w . x"]
2.75
% [nap "sum(w * x)"]; # same as this
2.75
% # i.e. weighted-sum or 'vector dot-product'

Multiple regression

The following defines the least-squares straight line using function regression:
% nap "x = {1 3 4 6 8 9 11 14}"
::NAP::14-14
% nap "y = {1 2 4 4 5 7  8  9}"
::NAP::16-16
% nap "c = regression(x, y)"
::NAP::31-31
% $c
0.545455 0.636364
% nap "y_intercept = c(0)"
::NAP::34-34
% nap "slope = c(1)"
::NAP::37-37
% nap "yy = y /// y_intercept + slope * x"; # actual = row 0, predicted = row 1
::NAP::42-42
% $yy set coo "" x
% $yy a
::NAP::42-42  f64  MissingValue: NaN  References: 1  Unit: (NULL)
Dimension 0   Size: 2      Name: (NULL)    Coordinate-variable: (NULL)
Dimension 1   Size: 8      Name: x         Coordinate-variable: ::NAP::14-14
Value:
1.00000 2.00000 4.00000 4.00000 5.00000 7.00000 ..
1.18182 2.45455 3.09091 4.36364 5.63636 6.27273 ..
% plot_nao yy -dash {{ } {}} -symbols {circle {}}
.win0

Author: Harvey Davies       © 2005, CSIRO Australia.       Legal Notice and Disclaimer
CVS Version Details: $Id: demo_linear.html,v 1.2 2005/03/15 00:57:34 dav480 Exp $