Skip to content

Commit a818852

Browse files
committed
new: Matrix multiplication example
1 parent 20f0461 commit a818852

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
34 KB
Binary file not shown.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
// query: Source
3+
section Section1;
4+
5+
Source =
6+
// query: Source. Shared input data
7+
[
8+
Matrix = #table(
9+
{"ColA", "ColB", "ColC", "ColD"}, {
10+
{3, 1, 4 ,3},
11+
{4, 3, 2 ,2},
12+
{5, 4, 4 ,1},
13+
{6, 5, 4 ,3},
14+
{7, 6, 2 ,1}
15+
}),
16+
Vector =
17+
#table({"ColA", "ColB", "ColC", "ColD"}, {
18+
{2, 3, 3, 2}
19+
})
20+
];
21+
22+
HardCoded =
23+
// query: Hardcoded Version
24+
let
25+
TableA = Source[Matrix],
26+
TableB = Source[Vector],
27+
28+
applyRow = (row as record, vector as any) => [
29+
vector =
30+
if vector is record then vector
31+
else Table.First( vector ),
32+
33+
return = [
34+
ColA = row[ColA] * vector[ColA],
35+
ColB = row[ColB] * vector[ColB],
36+
ColC = row[ColC] * vector[ColC],
37+
ColD = row[ColD] * vector[ColD],
38+
Sum = ColA + ColB + ColC + ColD
39+
][Sum]
40+
][return],
41+
42+
Result = Table.TransformRows(
43+
TableA,
44+
(row) => applyRow( row, TableB )
45+
),
46+
47+
Summary = [
48+
Matrix = Source[Matrix],
49+
Vector = Source[Vector],
50+
Result = Result,
51+
Sum = List.Sum( Result )
52+
]
53+
in
54+
Summary;
55+
56+
Dynamic =
57+
// query: Dynamic number of columns version
58+
let
59+
TableA = Source[Matrix],
60+
TableB = Source[Vector],
61+
// TableB = Table.SelectColumns( Source[Vector], {"ColA", "ColB"} ), // uncomment to trigger error on purpose
62+
63+
64+
// multiply a table matrix with by vector
65+
// vector can be a table or record
66+
MatrixVectorDot = (matrix as table, vector as any) => [
67+
vector = // ensure it's a record
68+
if vector is record then vector
69+
else Table.First( vector ),
70+
71+
vectorList = List.Buffer( Record.ToList( vector ) ),
72+
vectorLen = List.Count( vectorList ),
73+
74+
applyRows = Table.TransformRows(
75+
matrix,
76+
(row) => calcRow( row )
77+
),
78+
79+
calcRow = (row as record) as number => [
80+
rowList = Record.ToList( row ),
81+
rowLen = List.Count( rowList ),
82+
products = List.Transform(
83+
{ 0 .. ( vectorLen - 1 ) },
84+
(pos) =>
85+
rowList{pos} * vectorList{pos}
86+
),
87+
88+
return =
89+
if rowLen <> vectorLen then error [
90+
// allow them to drill down and inspect exact values that caused the error
91+
Message.Format = "Matrix row and Vector Len are not equal! row #{0} and Vector #{1}",
92+
Message.Parameters = { rowList, vectorList }
93+
] else
94+
List.Sum( products )
95+
][return],
96+
return = applyRows
97+
][return],
98+
// ], // toggle to debug/inspect every row's value
99+
100+
Summary = [
101+
TableA = TableA,
102+
TableB = TableB,
103+
Result = MatrixVectorDot( TableA, TableB ),
104+
FinalValue = List.Sum( Result )
105+
]
106+
in
107+
Summary;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
let
2+
// multiply a table matrix with by vector
3+
// vector can be a table or record
4+
// example: <https://github.com/ninmonkey/Ninmonkey.PowerQueryLib/tree/main/Examples/For-Loops/Matrix%20and%20Vector%20Multiplication.pq>
5+
MatrixVectorDot = (matrix as table, vector as any) => [
6+
vector = // ensure it's a record
7+
if vector is record then vector
8+
else Table.First( vector ),
9+
10+
vectorList = List.Buffer( Record.ToList( vector ) ),
11+
vectorLen = List.Count( vectorList ),
12+
13+
applyRows = Table.TransformRows(
14+
matrix,
15+
(row) => calcRow( row )
16+
),
17+
18+
calcRow = (row as record) as number => [
19+
rowList = Record.ToList( row ),
20+
rowLen = List.Count( rowList ),
21+
products = List.Transform(
22+
{ 0 .. ( vectorLen - 1 ) },
23+
(pos) =>
24+
rowList{pos} * vectorList{pos}
25+
),
26+
27+
return =
28+
if rowLen <> vectorLen then error [
29+
// allow them to drill down and inspect exact values that caused the error
30+
Message.Format = "Matrix row and Vector Len are not equal! row #{0} and Vector #{1}",
31+
Message.Parameters = { rowList, vectorList }
32+
] else
33+
List.Sum( products )
34+
][return],
35+
return = applyRows
36+
][return]
37+
in
38+
MatrixVectorDot

0 commit comments

Comments
 (0)