Topic: Expressions
Online Help
Vectors
Internal implementation and types of vectors
There are two types of vectors in Calcpad: regular (small) and large. Vectors can contain only real numbers with units. Complex vectors are not supported in this version. A single vector can contain different types of units, even if not consistent. However, some vector functions or operators may fail due to units’ inconsistency between separate elements.
Vectors with lengths that are greater than 100 are created as "large". Externally they behave just as regular vectors, so the user cannot make any difference. But internally, they operate quite differently. The structure of a large vector is displayed on the figure below:
A vector is defined with its full "mathematical" length, but no memory is initially reserved for it. This length can be obtained by the len(⃗v) function. The greatest index of a non-zero element defines the internal size of a vector. It is returned by the size(⃗v) function. The rest of the elements are known to be zero, so Calcpad does not need to store them in memory. Instead, it returns directly zero, if such an element is accessed.
This allows the software to work efficiently with vectors that are not entirely filled. Such vectors are very common in engineering as is the load vector in finite element analyses. However, Calcpad reserves a little bit more memory above the size, that is called "capacity". This is because resizing a vector is computationally expensive. Since we normally assign elements in a loop, in this way we avoid resizing the vector on each iteration.
Definition
Vectors can be defined by using the following syntax:
⃗a = [a1; a2; a3; … ;ai; … ;an]
The values of the separate elements can be specified by expressions that include variables, operators, functions, etc. For example:
a = [cos(0); 2; 3; 2*2; 6 - 1]
' = [1 2 3 4 5].
You can also include other vectors in the list. Their elements will be included in the sequence at the respective place, as follows:
b = [0; a; 6; 7; 8]
' = [0 1 2 3 4 5 6 7 8].
If you include matrices, they will be linearized to vectors by augmenting all rows one after the other. Vectors can be also defined as functions that will create them dynamically, depending on certain input parameters. For example:
a(x) = [1; x; x^2; x^3; x^4]
a(2)
' = [1 2 4 8 16]
Besides square brackets, you can also define vectors by using creational functions, as follows:
a = vector(5)
' = [0 0 0 0 0] - creates an empty vector with 5 elements;
fill(a; 5)
' = [5 5 5 5 5] - fills the vector with a value of 5;
a = range(0; 10; 2)
' = [0 2 4 6 8 10] - creates a vector from a range of values starting from 0 to 10 with step 2.
Indexing
You can access individual elements of a vector for reading and writing by using indexes. You have to specify the vector name, followed by a dot "." and the index after that. The first element has an index of one. The index can be a number, a single variable or expression. In the last case, the expression must be enclosed by brackets. For example:
a = [2; 4; 6; 8; 10]
a.2
' = 4
k = 3', 'a.k
' = ⃗a3 = 6
a.(2*k - 1)
' = ⃗a5 = 10
If an index value is less than 1 or greater than the vector length len(⃗a), the program will return an error: Index out of range. You can use indexing to initialize vectors inside loops (block or inline). For that purpose, you must include the loop counter into the index. For example:
a = vector(6)','b = vector(6)
'Block loop
#for k = 1 : len(a)
a.k = k^2
#loop
'Inline loop
$Repeat{b.k = a.(k - 1) @ k = 2 : len(b)}
The above code will produce the following two vectors:
⃗a = [1 4 9 16 25 36] and
⃗b = [0 1 4 9 16 25].
Structural functions
This includes all functions that read or modify the structure of the vector. It means that the result does not depend on the content, i.e. the values of elements. The following functions are available in Calcpad:
len(⃗a)
Parameters: ⃗a - vector.
Return value: (scalar) the length of vector ⃗a.
Notes: Represents the full length of the vector (in respect to element count).
Example: len([1; 0; 2; 3])
' = 4
size(⃗a)
Parameters: ⃗a - vector.
Return value: (scalar) the internal size of vector ⃗a.
Notes: If ⃗a is a large vector, returns the index of the last non-zero element, else - returns the vector length.
Example: a = vector(200)
' = 200
a.35 = 1
len(a) size(a)
' = 35size([1; 2; 3; 0; 0])
' = 5
resize(⃗a; n)
Parameters: ⃗a - vector;
n - (positive integer) the new length of vector ⃗a.
Return value: the resized vector ⃗a.
Notes: Sets a new length n of vector ⃗a by modifying the vector in place and returns a reference to the same vector as a result.
Example: a = [1; 2; 3; 4; 5]
' = [1 2 3]
b = resize(a; 3) a
' = [1 2 3]
join(A; ⃗b; c…)
Parameters: a list of matrices, vectors and scalars.
Return value: a new vector, obtained by joining the arguments in the list.
Notes: The list can include unlimited number of items of different types, mixed arbitrarily. Matrices are first linearized by rows and their elements are included into the common sequence, as well as the vectors, each at its place.
Example: A = [1; 2|3; 4]
' = [0 1 2 3 4 5 6 7 8 9]
b = [7; 8; 9]
c = join(0; A; 5; 6; b)
slice(⃗a; i1; i2)
Parameters: ⃗a - vector;
i1 - (positive integer) starting index;
i2 - (positive integer) ending index.
Return value: a new vector, containing the part of vector ⃗a bounded by indexes i1 and i2, inclusively.
Notes: It is not required that i1 ≤ i2. If an index is greater than the vector length, then all remaining elements are returned to the end.
Example: slice([1; 2; 3; 4; 5; 6; 7; 8]; 3; 7)
' = [3 4 5 6 7]slice([1; 2; 3; 4; 5; 6; 7; 8]; 6; 10)
' = [6 7 8]
first(⃗a; n)
Parameters: ⃗a - vector;
n - (positive integer) the number of elements to return.
Return value: a vector containing the first n elements of ⃗a.
Notes: If n is greater than the length of ⃗a, then all elements are returned. Unlike resize the original vector is not modified.
Example: first([0; 1; 2; 3; 4; 5]; 3)
' = [0 1 2]first([0; 1; 2; 3; 4; 5]; 10)
' = [0 1 2 3 4 5]
last(⃗a; n)
Parameters: ⃗a - vector;
n - (positive integer) the number of elements to return.
Return value: a vector containing the last n elements of ⃗a.
Notes: If n is greater than the length of ⃗a, then all elements are returned.
Example: last([0; 1; 2; 3; 4; 5]; 3)
' = [3 4 5]last([0; 1; 2; 3; 4; 5]; 10)
' = [0 1 2 3 4 5]
extract(⃗a; ⃗i)
Parameters: ⃗a - a vector containing the elements to be extracted;
⃗i - a vector with the indexes of the elements to be extracted from ⃗a.
Return value: a vector with the extracted elements from ⃗a which indexes are provided in ⃗i.
Notes: All indexes in ⃗i must be positive integers. If an index is greater than the length of vector ⃗a, an "Index out of range" error is returned.
Example: a = [0; 1; 2; 3; 4; 5; 6]
extract(a; [2; 4; 6])
' = [1 3 5]
Data functions
This kind of functions treat the vector contents as numerical data. They are related mainly to sorting, ordering, searching and counting. Unlike structural functions, the result depends on the element values. You can use the following functions:
sort(⃗a)
Parameters: ⃗a - input vector.
Return value: a vector containing the elements of ⃗a, sorted in ascending order.
Notes: The original content of ⃗a is not modified.
Example: a = [4; 0; 2; 3; -1; 1]
' = [-1 0 1 2 3 4]
b = sort(a)a
' = [4 0 2 3 -1 1]
rsort(⃗a)
Parameters: ⃗a - input vector.
Return value: a vector containing the elements of ⃗a, sorted in descending order.
Notes: Similar to sort, the original content of ⃗a remains unchanged.
Example: rsort([4; 0; 2; 3; -1; 1])
' = 4 3 2 1 0 -1]
order(⃗a)
Parameters: ⃗a - input vector.
Return value: a vector with indexes, ordered by the elements of ⃗a, ascending.
Notes: Each index in the output vector ⃗i shows which element in ⃗a should be placed at the current position to obtain a sorted sequence. You can do that by calling extract ( ⃗a; ⃗i ).
Example: a = [4; 0; 2; 3; -1; 1]
' = [5 2 6 3 4 1]
i = order(a) b = extract(a; i)
' = [-1 0 1 2 3 4]
revorder(⃗a)
Parameters: ⃗a - input vector.
Return value: a vector with indexes, ordered by the elements of ⃗a, descending.
Notes: The same considerations as for the order function apply.
Example: revorder([4; 0; 2; 3; -1; 1])
' = [1 4 3 6 2 5]
reverse(⃗a)
Parameters: ⃗a - input vector.
Return value: a vector containing the elements of ⃗a in reverse order.
Notes: The original content of ⃗a remains unchanged.
Example: reverse([1; 2; 3; 4; 5])
' = [5 4 3 2 1]
count(⃗a; x; i)
Parameters: ⃗a - vector;
x - (scalar) the value to count;
i - (positive integer) the index to start with.
Return value: (scalar) the number of elements in ⃗a, after the i-th incl., that are equal to x.
Notes: If i is greater than the length of ⃗a, then zero is returned.
Example: count([0; 1; 2; 1; 4; 1]; 1; 4)
' = 2
search(⃗a; x; i)
Parameters: ⃗a - vector;
x - (scalar) the value to search for;
i - (positive integer) the index to start with.
Return value: (scalar) the index of the first element in ⃗a, after the i-th incl., that is equal to x.
Notes: If i is greater than the length of ⃗a or the value is not found, zero is returned.
Example: search([0; 1; 2; 1; 4; 1]; 1; 3)
' = 4search([0; 1; 2; 1; 4; 1]; 1; 7)
' = 0
find(⃗a; x; i)
Parameters: ⃗a - vector;
x - (scalar) the value to search for;
i - (positive integer) the index to start with.
Return value: a vector with the indexes of all elements in ⃗a, after the i-th incl., that are equal to x.
Notes: If i is greater than the length of ⃗a or the value is not found, an empty vector is returned (with zero length).
Example: find([0; 1; 2; 1; 4; 1]; 1; 2)
' = [2 4 6]find([0; 1; 2; 1; 4; 1]; 3; 2)
' = []
lookup(⃗a; ⃗b; x)
Parameters: ⃗a - vector with reference values;
⃗b - vector with return values;
x - (scalar) the value to look for.
Return value: a vector with all elements in ⃗b, for which the corresponding elements in
⃗a are equal to x.
Notes: if the value is not found, an empty vector is returned (with zero length)
Example: a = [0; 1; 0; 0; 1; 1]
' = [1 3 4]
b = [1; 2; 3; 4; 5; 6]
lookup(a; b; 0)lookup(a; b; 2)
' = []
The find and lookup functions have variations with suffixes. Different suffixes refer to different comparison operators. They replace the equality in the original functions while the other behavior remains unchanged. The possible suffixes are given in the table below:
suffix | find | lookup | comparison operator |
---|---|---|---|
_eq |
find_eq(⃗a; x; i) |
lookup_eq(⃗a; ⃗b; x) |
= - equal |
_ne |
find_ne(⃗a; x; i) |
lookup_ne(⃗a; ⃗b; x) |
≠ - not equal |
_lt |
find_lt(⃗a; x; i) |
lookup_lt(⃗a; ⃗b; x) |
< - less than |
_le |
find_le(⃗a; x; i) |
lookup_le(⃗a; ⃗b; x) |
≤ - less than or equal |
_gt |
find_gt(⃗a; x; i) |
lookup_gt(⃗a; ⃗b; x) |
> - greater than |
_ge |
find_ge(⃗a; x; i) |
lookup_ge(⃗a; ⃗b; x) |
≥ - greater than or equal |
Math functions
All standard scalar math functions accept vector arguments as well. The function is applied separately to each of the elements in the input vector and the results are returned in a corresponding output vector. For example:
sin([0; 30; 45; 90])
' = [0 0.5 0.707 1]
Calcpad also includes several math functions that are specific for vectors:
norm_p(⃗a)
Parameters: ⃗a - vector.
Return value: scalar representing the Lp norm of vector ⃗a.
Notes: The Lp norm is obtained by the formula: ||⃗a||p = (n∑i = 1| ai |p)1/p.
Example: norm_p([1; 2; 3]; 3)
' = 3.3019
norm_1(⃗a)
Parameters: ⃗a - vector.
Return value: scalar representing the L1 norm of vector ⃗a.
Notes: The L1 norm is obtained by the formula: ||⃗a||1 = n∑i = 1| ai |.
Example: norm_1([-1; 2; 3])
' = 6
norm(⃗a)
or norm_2(⃗a)
or norm_e(⃗a)
Parameters: ⃗a - vector.
Return value: scalar representing the L2 (Euclidian) norm of vector ⃗a.
Notes: The L2 norm is obtained by the formula: ||⃗a||2 = n∑i = 1ai2.
Example: norm_2([1; 2; 3])
' = 3.7417
norm_i(⃗a)
Parameters: ⃗a - vector.
Return value: scalar representing the L∞ (infinity) norm of vector ⃗a.
Notes: The L∞ norm is obtained by the formula: ||⃗a||∞ = max | ai |.
Example: norm_i([1; 2; 3]; 3)
' = 3
unit(⃗a)
Parameters: ⃗a - vector.
Return value: the normalized vector ⃗a (with L2 norm ||⃗a||2 = 1).
Notes: The elements of the normalized vector ⃗u are evaluated by the expression: ui = ai / ||a||2
Example: unit([1; 2; 3])
' = [0.26726 0.53452 0.80178]
dot(⃗a; ⃗b)
Parameters: ⃗a, ⃗b - vectors.
Return value: scalar representing the dot product of both vectors ⃗a ·⃗b;
Notes: The dot product is obtained by the expression: ⃗a ·⃗b = n∑i = 1 ai ·bi
Example: a = [1; 2; 4]
' = 15
b = [5; 3; 1]
dot(a; b)
cross(⃗a; ⃗b)
Parameters: ⃗a, ⃗b - vectors.
Return value: vector representing the cross product ⃗c = ⃗a × ⃗b.
Notes: This function is defined only for vectors with lengths 2 or 3.The elements of the resulting vector ⃗c are calculated as follows:
c1 = a2 b3 − a3 b2
c2 = a3 b1 − a1 b3
c3 = a1 b2 − a2 b1
Example: a = [1; 2; 4]
' = [-10 19 -7]
b = [5; 3; 1]
cross(a; b)
Aggregate and interpolation functions
All aggregate functions can work with vectors. Since they are multivariate, each of them can accept a single vector, but also a list of scalars, vectors and matrices, mixed in arbitrary order. In this case, all arguments are merged into a single array of scalars, consecutively from left to right. For example:
a = [0; 2; 6]
b = [5; 3; 1]
sum(10; a; b; 11)
' = 38
Interpolation functions behave similarly, but the first argument must be scalar, that represents the interpolation variable. For example:
take(3; a)
' = 6
line(1.5; a)
' = 1
spline(1.5; a)
' = 0.8125
Like aggregate functions, interpolation functions also accept mixed lists of arguments, as follows:
a = [1; 2; 3]
b = [5; 6; 7; 8]
take(7; a; 4; b; 9; 10)
' = 7
The returned value is actually the third element in vector ⃗b, but it has an index 7 in the final sequence. A full list of the available aggregate and interpolation functions is provided earlier in this manual (see "Expressions/Functions" above).
Operators
All operators can work with vector operands. Operations are performed element-by-element and the results are returned in an output vector. This applies also for the multiplication operator. For example:
[2; 4; 5]*[2; 3; 4]
' = [4 12 20]
If the lengths of both vectors are different, the shorter vector is padded with zeros to the length of the longer one. Dot and cross products in Calcpad are implemented as functions (see above). All binary operators are supported for vector-scalar and scalar-vector operands in a similar way. For example:
[2; 4; 5]*2
' = [4 8 10]
Table of contents
-
+
About Calcpad
-
+
Writing code
-
+
Coding aids
-
−
Expressions
-
+
Reporting
-
+
Programming
-
+
Results
-
+
Working with files