Basic Operators and Functions for Vectors
Operators and functions for vectors operate element-wise.
Basic operators for vectors
The basic operators +, -, *, and / work for vectors of variables and expressions. These operators are applied element-wise.
Vector–Scalar Operations
When you combine a vector and a scalar, the scalar is applied to each element of the vector.
The following program illustrates this behavior:
from pyqbpp import var
x = var("x", 3)
f = 2 * x + 1
print("f =", f)
for i in range(len(f)):
print(f"f[{i}] =", f[i])
This program produces the following output:
f = {1 +2*x[0],1 +2*x[1],1 +2*x[2]}
f[0] = 1 +2*x[0]
f[1] = 1 +2*x[1]
f[2] = 1 +2*x[2]
Vector–Vector Operations
When you combine two vectors of the same size, the operation is performed element-wise.
from pyqbpp import var
x = var("x", 3)
y = var("y", 3)
f = 2 * x + 3 * y + 1
print("f =", f)
for i in range(len(f)):
print(f"f[{i}] =", f[i])
This program produces the following output:
f = {1 +2*x[0] +3*y[0],1 +2*x[1] +3*y[1],1 +2*x[2] +3*y[2]}
f[0] = 1 +2*x[0] +3*y[0]
f[1] = 1 +2*x[1] +3*y[1]
f[2] = 1 +2*x[2] +3*y[2]
Compound operators for vectors
The compound operators +=, -=, *=, and /= also work element-wise for vectors:
from pyqbpp import var
x = var("x", 3)
y = var("y", 3)
f = 6 * x + 4
f += 3 * y
print("f =", f)
f -= 12
print("f =", f)
f *= 2 * y
print("f =", f)
f /= 2
print("f =", f)
This program produces the following output:
f = {4 +6*x[0] +3*y[0],4 +6*x[1] +3*y[1],4 +6*x[2] +3*y[2]}
f = {-8 +6*x[0] +3*y[0],-8 +6*x[1] +3*y[1],-8 +6*x[2] +3*y[2]}
f = {12*x[0]*y[0] +6*y[0]*y[0] -16*y[0],12*x[1]*y[1] +6*y[1]*y[1] -16*y[1],12*x[2]*y[2] +6*y[2]*y[2] -16*y[2]}
f = {6*x[0]*y[0] +3*y[0]*y[0] -8*y[0],6*x[1]*y[1] +3*y[1]*y[1] -8*y[1],6*x[2]*y[2] +3*y[2]*y[2] -8*y[2]}
Square function for vectors
The square function also works element-wise for vectors:
from pyqbpp import var, sqr
x = var("x", 3)
f = sqr(x + 1)
print("f =", f)
This program produces the following output:
f = {1 +x[0]*x[0] +x[0] +x[0],1 +x[1]*x[1] +x[1] +x[1],1 +x[2]*x[2] +x[2] +x[2]}
Simplify functions for vectors
Simplify functions also work element-wise for vectors:
from pyqbpp import var, sqr, simplify, simplify_as_binary, simplify_as_spin
x = var("x", 3)
f = sqr(x - 1)
print("f =", f)
print("simplify(f) =", simplify(f))
print("simplify_as_binary(f) =", simplify_as_binary(f))
print("simplify_as_spin(f) =", simplify_as_spin(f))
This program produces the following output:
f = {1 +x[0]*x[0] -x[0] -x[0],1 +x[1]*x[1] -x[1] -x[1],1 +x[2]*x[2] -x[2] -x[2]}
simplify(f) = {1 -2*x[0] +x[0]*x[0],1 -2*x[1] +x[1]*x[1],1 -2*x[2] +x[2]*x[2]}
simplify_as_binary(f) = {1 -x[0],1 -x[1],1 -x[2]}
simplify_as_spin(f) = {2 -2*x[0],2 -2*x[1],2 -2*x[2]}
NOTE These operators and functions also work for multi-dimensional arrays.