Getting started
Vectors & matrices
basic vector and matrix arithmetic.
catalog/demo_vectors.qu
A first look at Qu's arrays: a vector, a matrix, and the difference between `*` (matrix multiply) and `.*` (elementwise).
What it exercises
- vector literals, dot(), matrix literals, * vs .*, transpose
How it works
Vectors
u = [1, 2, 3]
v = [4, 5, 6]
print("u . v = {dot(u, v)}")
print("u + v = {u + v}")
Matrices
A = [1, 2; 3, 4]
B = [5, 6; 7, 8]
print("A * B = {A * B}")
print("A .* B = {A .* B}")
print("A' = {A'}")
Results
printed
u . v = 32 u + v = [5, 7, 9] A * B = [19, 22; 43, 50] A .* B = [5, 12; 21, 32] A' = [1, 3; 2, 4]
The whole program
Runnable as it stands: qu run catalog/demo_vectors.qu
# demo_vectors.qu — basic vector and matrix arithmetic.
#
# A first look at Qu's arrays: a vector, a matrix, and the difference
# between `*` (matrix multiply) and `.*` (elementwise).
#
# Exercises:
# vector literals, dot(), matrix literals, * vs .*, transpose
backend auto
#%% Vectors
u = [1, 2, 3]
v = [4, 5, 6]
print("u . v = {dot(u, v)}")
print("u + v = {u + v}")
#%% Matrices
A = [1, 2; 3, 4]
B = [5, 6; 7, 8]
print("A * B = {A * B}")
print("A .* B = {A .* B}")
print("A' = {A'}")