Skip to main content

+

Overloads

NameDescription
(const Object lhs + const RealTensor rhs) -> RealTensorElement-wise addition of Real tensors or tensor-scalar addition.
(const Object lhs + const ComplexTensor rhs) -> ComplexTensorElement-wise addition of Complex tensors or tensor-scalar addition.

(const Object lhs + const RealTensor rhs) -> RealTensor

Element-wise addition of Real tensors or tensor-scalar addition.

Adds two tensors element-wise, or adds a scalar to a tensor.

For tensor-tensor addition:

  • Both tensors must have the same shape
  • Result[i,j,k,...] = A[i,j,k,...] + B[i,j,k,...]

For tensor-scalar addition:

  • Every element of the tensor is increased by the scalar
  • Result[i,j,k,...] = A[i,j,k,...] + s

This operation is commutative: A + B = B + A, and scalar + tensor = tensor + scalar.

Parameters

  • rhs: Second operand: real tensor or scalar.

Returns

A new real tensor containing the element-wise sum.

Example

// Add two tensors
var A = filled_tensor([2, 3], 1.0, as_real)
var B = filled_tensor([2, 3], 2.0, as_real)
var C = A + B // Each element is 1.0 + 2.0 = 3.0
print(C[0, 0]) // 3.0

// Add scalar to tensor
var D = filled_tensor([2, 3], 5.0, as_real)
var E = D + 3.0 // Each element is 5.0 + 3.0 = 8.0
print(E[1, 1]) // 8.0

// Scalar-tensor addition (commutative)
var F = 3.0 + D // Same as D + 3.0
print(F[0, 0]) // 8.0

(const Object lhs + const ComplexTensor rhs) -> ComplexTensor

Element-wise addition of Complex tensors or tensor-scalar addition.

Adds two tensors element-wise, or adds a scalar to a tensor.

For tensor-tensor addition:

  • Both tensors must have the same shape
  • Result[i,j,k,...] = A[i,j,k,...] + B[i,j,k,...]

For tensor-scalar addition:

  • Every element of the tensor is increased by the scalar
  • Result[i,j,k,...] = A[i,j,k,...] + s

This operation is commutative: A + B = B + A, and scalar + tensor = tensor + scalar.

Parameters

  • rhs: Second operand: complex tensor or scalar.

Returns

A new complex tensor containing the element-wise sum.

Example

// Add two tensors
var A = filled_tensor([2, 3], 1.0, as_complex)
var B = filled_tensor([2, 3], 2.0, as_complex)
var C = A + B // Each element is 1.0 + 2.0 = 3.0
print(C[0, 0]) // 3.0

// Add scalar to tensor
var D = filled_tensor([2, 3], 5.0, as_complex)
var E = D + 3.0 // Each element is 5.0 + 3.0 = 8.0
print(E[1, 1]) // 8.0

// Scalar-tensor addition (commutative)
var F = 3.0 + D // Same as D + 3.0
print(F[0, 0]) // 8.0