operator_expand
Automatic expansion of singleton dimensions in binary operators
Syntax
X op Y
Arguments
- X, Y
Scalars, vectors, matrices or hypermatrices of numbers (real or complex), encoded integers, polynomials or texts, according to the operator being applied.
Description
Expanding automatically replicates any dimension equal to 1
so that both operands get identical shapes before applying an element-wise binary
operator. Missing trailing dimensions are considered to be 1.
The resulting size is the per-dimension maximum of both shapes.
A scalar is just a special case where all dimensions are singletons, so scalar expansion is naturally covered by expanding.
Expanding is currently implemented for the following operators:
+ (addition), - (subtraction),
.* (element-wise multiplication),
./ (element-wise right division),
and .^ (element-wise power).
Examples
// Addition with expand a = [1; 2]; b = [10 20 30]; a + b
--> a = [1; 2]; --> b = [10 20 30]; --> a + b ans = 11. 21. 31. 12. 22. 32.
// Subtraction with expand a = [1; 2]; b = [10 20 30]; b - a
--> b - a ans = 9. 19. 29. 8. 18. 28.
// Element-wise multiplication with expand a = [1; 2; 3]; b = [10 20]; a .* b
--> a = [1; 2; 3]; --> b = [10 20]; --> a .* b ans = 10. 20. 20. 40. 30. 60.
// Element-wise division with expand a = [100; 200]; b = [1 2 4]; a ./ b
--> a = [100; 200]; --> b = [1 2 4]; --> a ./ b ans = 100. 50. 25. 200. 100. 50.
// Element-wise power with expand a = [2; 3]; b = [1 2 3]; a .^ b
--> a = [2; 3]; --> b = [1 2 3]; --> a .^ b ans = 2. 4. 8. 3. 9. 27.
// Complex doubles ca = [1+%i; -2+2*%i]; cb = [%i, 2-%i, -1+3*%i]; ca + cb
--> ca = [1+%i; -2+2*%i]; --> cb = [%i, 2-%i, -1+3*%i]; --> ca + cb ans = 1. + 2.i 3. + 0.i 0. + 4.i -2. + 3.i 0. + i -3. + 5.i
// Polynomials pcol = [%s; 1 + %s]; prow = [%s^2, 2 - %s]; pcol + prow
--> pcol = [%s; 1 + %s]; --> prow = [%s^2, 2 - %s]; --> pcol + prow ans = s +s^2 2 1. +s +s^2 3
// Incompatible shapes still fail [1 2; 3 4] + ones(3, 2)
--> [1 2; 3 4] + ones(3, 2) Operator +: Wrong dimensions for operation [2x2] + [3x2], same dimensions expected.
See also
- addition — Numerical addition. Text concatenation (gluing)
- subtraction — (-) subtraction operator. Sign change
- multiplication — (*) multiplication operator
- division — (/) right divisions. System's feed back. Comments
- power — (^) exponentiation
- operators — scilab operator names
History
| Version | Description |
| 2026.1.0 | Feature added. |
| Report an issue | ||
| << not | Scilab keywords | parentheses >> |