Struct concrete_core::math::tensor::Tensor[][src]

#[repr(transparent)]
pub struct Tensor<Container: ?Sized>(_);
Expand description

A generic type to perform operations on collections of scalar values.

See the module-level documentation for more explanations on the logic of this type.

Naming convention

The methods that may mutate the values of a Tensor, follow a convention:

  • Methods prefixed with update_with use the current values of self when performing the operation.
  • Methods prefixed with fill_with discard the current vales of self, and overwrite it with the result of an operation on other values.

Implementations

Allocates a new Tensor<Vec<T>> whose values are all value.

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
assert_eq!(tensor.len(), 1000);
assert_eq!(*tensor.get_element(0), 9);
assert_eq!(*tensor.get_element(1), 9);

Saves a tensor to a binary file.

Note

The file format does not contain a type signature that guarantees that the file was restored to the same type it was saved.

Example

use concrete_core::math::tensor::Tensor;
use std::path::PathBuf;
let t = Tensor::allocate(66 as u8, 10_000);
let path = PathBuf::from("/tmp/test_save_tensor.ts");
assert!(!path.exists());
t.save_to_file(&path);
assert!(path.exists());

Loads a tensor from a binary file.

Note

The file format does not contain a type signature that guarantees that the file was restored to the same type it was saved.

Example

use concrete_core::math::tensor::Tensor;
use std::path::PathBuf;
let t_initial = Tensor::allocate(66 as u8, 10_000);
let path = PathBuf::from("/tmp/test_save_tensor.ts");
t_initial.save_to_file(&path);
let t_recovered = Tensor::load_from_file(&path).unwrap();
assert_eq!(t_initial, t_recovered);

Creates a new tensor from a container.

Example

use concrete_core::math::tensor::Tensor;
let vec = vec![9 as u8; 1000];
let view = vec.as_slice();
let tensor = Tensor::from_container(view);
assert_eq!(tensor.len(), 1000);
assert_eq!(*tensor.get_element(0), 9);
assert_eq!(*tensor.get_element(1), 9);

Consumes a tensor and returns its container.

Example

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
let vec = tensor.into_container();
assert_eq!(vec.len(), 1000);
assert_eq!(vec[0], 9);
assert_eq!(vec[1], 9);

Returns a reference to the tensor container.

Example

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
let vecref: &Vec<_> = tensor.as_container();

Returns a mutable reference to the tensor container.

Example

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
let vecmut: &mut Vec<_> = tensor.as_mut_container();

Returns the length of the tensor.

Example

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
assert_eq!(tensor.len(), 1000);

Returns whether the tensor is empty.

Example

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
assert_eq!(tensor.is_empty(), false);

Returns an iterator over &Scalar elements.

Example

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
for scalar in tensor.iter() {
    assert_eq!(*scalar, 9);
}

Returns an iterator over &mut T elements.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
for mut scalar in tensor.iter_mut() {
    *scalar = 8;
}
for scalar in tensor.iter() {
    assert_eq!(*scalar, 8);
}

Returns an iterator over sub tensors Tensor<&[Scalar]>.

Note:

The length of the sub-tensors must divide the size of the tensor.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
for sub in tensor.subtensor_iter(10) {
    assert_eq!(sub.len(), 10);
}

Returns an iterator over mutable sub tensors Tensor<&mut [Scalar]>.

Note:

The length of the sub-tensors must divide the size of the tensor.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
for mut sub in tensor.subtensor_iter_mut(10) {
    assert_eq!(sub.len(), 10);
    *sub.get_element_mut(0) = 1;
}
for sub in tensor.subtensor_iter(20) {
    assert_eq!(*sub.get_element(0), 1);
    assert_eq!(*sub.get_element(10), 1);
}

Returns a reference to the first element.

Note:

Panics if the tensor is empty.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
assert_eq!(*tensor.first(), 9);

Returns a reference to the last element.

Note:

Panics if the tensor is empty.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
assert_eq!(*tensor.last(), 9);

Returns a mutable reference to the first element.

Note:

Panics if the tensor is empty.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
*tensor.first_mut() = 8;
assert_eq!(*tensor.get_element(0), 8);
assert_eq!(*tensor.get_element(1), 9);

Returns a mutable reference to the last element.

Note:

Panics if the tensor is empty.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
*tensor.last_mut() = 8;
assert_eq!(*tensor.get_element(999), 8);
assert_eq!(*tensor.get_element(1), 9);

Returns a reference to the first element, and a ref tensor for the rest of the values.

Note:

Panics if the tensor is empty.

Example:

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
let (first, end) = tensor.split_first();
assert_eq!(*first, 9);
assert_eq!(end.len(), 999);

Returns a reference to the last element, and a ref tensor to the rest of the values.

Note:

Panics if the tensor is empty.

Example:

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
let (last, beginning) = tensor.split_last();
assert_eq!(*last, 9);
assert_eq!(beginning.len(), 999);

Returns a mutable reference to the first element, and a mut tensor for the rest of the values.

Note:

Panics if the tensor is empty.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
let (mut first, mut end) = tensor.split_first_mut();
*first = 8;
*end.get_element_mut(0) = 7;
assert_eq!(*tensor.get_element(0), 8);
assert_eq!(*tensor.get_element(1), 7);
assert_eq!(*tensor.get_element(2), 9);

Returns a mutable reference to the last element, and a mut tensor for the rest of the values.

Note:

Panics if the tensor is empty.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
let (mut last, mut beginning) = tensor.split_last_mut();
*last = 8;
*beginning.get_element_mut(0) = 7;
assert_eq!(*tensor.get_element(0), 7);
assert_eq!(*tensor.get_element(999), 8);
assert_eq!(*tensor.get_element(2), 9);

Returns a sub tensor from a range of indices.

Note:

Panics if the indices are out of range.

Example:

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
let sub = tensor.get_sub(0..3);
assert_eq!(sub.len(), 3);

Returns a mutable sub tensor from a range of indices.

Note:

Panics if the indices are out of range.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
let mut sub = tensor.get_sub_mut(0..3);
sub.fill_with_element(0);
assert_eq!(*tensor.get_element(0), 0);
assert_eq!(*tensor.get_element(3), 9);

Returns a reference to an element from an index.

Note:

Panics if the index is out of range.

Example:

use concrete_core::math::tensor::Tensor;
let tensor = Tensor::allocate(9 as u8, 1000);
assert_eq!(*tensor.get_element(0), 9);

Returns a mutable reference to an element from an index.

Note:

Panics if the index is out of range.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
*tensor.get_element_mut(0) = 8;
assert_eq!(*tensor.get_element(0), 8);
assert_eq!(*tensor.get_element(1), 9);

Sets the value of an element at a given index.

Note:

Panics if the index is out of range.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
*tensor.get_element_mut(0) = 8;
assert_eq!(*tensor.get_element(0), 8);
assert_eq!(*tensor.get_element(1), 9);

Fills a tensor with the values of another tensor, using memcpy.

Example

use concrete_core::math::tensor::Tensor;
let mut tensor1 = Tensor::allocate(9 as u8, 1000);
let tensor2 = Tensor::allocate(10 as u8, 1000);
tensor1.fill_with_copy(&tensor2);
assert_eq!(*tensor2.get_element(0), 10);

Fills two tensors with the result of the operation on a single one.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor1 = Tensor::allocate(9 as u8, 1000);
let mut tensor2 = Tensor::allocate(9 as u8, 1000);
let tensor3 = Tensor::allocate(10 as u8, 1000);
Tensor::fill_two_with_one(&mut tensor1, &mut tensor2, &tensor3, |a| (*a, *a));
assert_eq!(*tensor1.get_element(0), 10);
assert_eq!(*tensor2.get_element(0), 10);

Fills a mutable tensor with the result of an element-wise operation on two other tensors of the same size

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(1 as u8, 1000);
let t3 = Tensor::allocate(2 as u8, 1000);
t1.fill_with_two(&t2, &t3, |t2, t3| t3 + t2);
for scalar in t1.iter() {
    assert_eq!(*scalar, 3);
}

Fills a mutable tensor with the result of an element-wise operation on one other tensor of the same size

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(2 as u8, 1000);
t1.fill_with_one(&t2, |t2| t2.pow(2));
for scalar in t1.iter() {
    assert_eq!(*scalar, 4);
}

Fills a mutable tensor with an element.

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
tensor.fill_with_element(8);
for scalar in tensor.iter() {
    assert_eq!(*scalar, 8);
}

Fills a mutable tensor by repeatedly calling a closure.

use concrete_core::math::tensor::Tensor;
use std::cell::RefCell;
let mut tensor = Tensor::allocate(9 as u16, 1000);
let mut boxed = RefCell::from(0);
tensor.fill_with(|| {
    *boxed.borrow_mut() += 1;
    *boxed.borrow()
});
assert_eq!(*tensor.get_element(0), 1);
assert_eq!(*tensor.get_element(1), 2);
assert_eq!(*tensor.get_element(2), 3);

Fills a mutable tensor by casting elements of another one.

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u16, 1000);
let mut t2 = Tensor::allocate(8. as f32, 1000);
t1.fill_with_cast(&t2);
for scalar in t1.iter() {
    assert_eq!(*scalar, 8);
}

Updates two tensors with the result of the operation with a single one.

Example:

use concrete_core::math::tensor::Tensor;
let mut tensor1 = Tensor::allocate(9 as u8, 1000);
let mut tensor2 = Tensor::allocate(9 as u8, 1000);
let tensor3 = Tensor::allocate(10 as u8, 1000);
Tensor::update_two_with_one(&mut tensor1, &mut tensor2, &tensor3, |a, b, c| {
    *a += *c;
    *b += *c + 1;
});
assert_eq!(*tensor1.get_element(0), 19);
assert_eq!(*tensor2.get_element(0), 20);

Updates a mutable tensor with the result of an element-wise operation with two other tensors of the same size.

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(1 as u8, 1000);
let t3 = Tensor::allocate(2 as u8, 1000);
t1.update_with_two(&t2, &t3, |t1, t2, t3| *t1 += t3 + t2);
for scalar in t1.iter() {
    assert_eq!(*scalar, 12);
}

Updates a mutable tensor with the result of an element-wise operation with one other tensor of the same size

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(2 as u8, 1000);
t1.update_with_one(&t2, |t1, t2| *t1 += t2.pow(2));
for scalar in t1.iter() {
    assert_eq!(*scalar, 13);
}

Updates a mutable tensor with an element.

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
tensor.update_with_element(8, |t, s| *t += s);
for scalar in tensor.iter() {
    assert_eq!(*scalar, 17);
}

Updates a mutable tensor by repeatedly calling a closure.

use concrete_core::math::tensor::Tensor;
use std::cell::RefCell;
let mut tensor = Tensor::allocate(9 as u16, 1000);
let mut boxed = RefCell::from(0);
tensor.update_with(|t| {
    *boxed.borrow_mut() += 1;
    *t += *boxed.borrow()
});
assert_eq!(*tensor.get_element(0), 10);
assert_eq!(*tensor.get_element(1), 11);
assert_eq!(*tensor.get_element(2), 12);

Sets each value of self to its own opposite.

Example

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as i16, 1000);
tensor.update_with_neg();
for scalar in tensor.iter() {
    assert_eq!(*scalar, -9);
}

Sets each value of self to its own wrapping opposite.

Example

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::allocate(9 as u8, 1000);
tensor.update_with_wrapping_neg();
for scalar in tensor.iter() {
    assert_eq!(*scalar, 247);
}

Fills a mutable tensor with the result of the multiplication of elements of another tensor by an element.

Example

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(3 as u8, 1000);
t1.fill_with_element_mul(&t2, 2);
for scalar in t1.iter() {
    assert_eq!(*scalar, 6);
}

Fills a mutable tensor with the result of the wrapping multiplication of elements of another tensor by an element.

Example

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(3 as u8, 1000);
t1.fill_with_wrapping_element_mul(&t2, 250);
for scalar in t1.iter() {
    assert_eq!(*scalar, 238);
}

Updates the values of a mutable tensor by subtracting the product of the element of another tensor and an element.

Example

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(2 as u8, 1000);
t1.update_with_sub_element_mul(&t2, 4);
for scalar in t1.iter() {
    assert_eq!(*scalar, 1);
}

Updates the values of a mutable tensor by wrap-subtracting the wrapping product of the elements of another tensor and an element.

Example

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(2 as u8, 1000);
t1.update_with_wrapping_sub_element_mul(&t2, 250);
for scalar in t1.iter() {
    assert_eq!(*scalar, 21);
}

Updates the values of a mutable tensor by adding the product of the element of another tensor and an element.

Example

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(2 as u8, 1000);
t1.update_with_add_element_mul(&t2, 4);
for scalar in t1.iter() {
    assert_eq!(*scalar, 17);
}

Updates the values of a mutable tensor by wrap-adding the wrapping product of the elements of another tensor and an element.

Example

use concrete_core::math::tensor::Tensor;
let mut t1 = Tensor::allocate(9 as u8, 1000);
let t2 = Tensor::allocate(2 as u8, 1000);
t1.update_with_wrapping_add_element_mul(&t2, 250);
for scalar in t1.iter() {
    assert_eq!(*scalar, 253);
}

Computes a value by folding a tensor with another.

Example

use concrete_core::math::tensor::Tensor;
let t1 = Tensor::allocate(10 as u16, 10);
let t2 = Tensor::allocate(2 as u16, 10);
let val = t1.fold_with_one(&t2, 0, |mut a, t1, t2| {
    a += t1 + t2;
    a
});
assert_eq!(val, 120);

Reverses the elements of the tensor inplace.

Example

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::from_container(vec![1u8, 2, 3, 4]);
tensor.reverse();
assert_eq!(*tensor.get_element(0), 4);

Rotates the elements of the tensor to the right, inplace.

Example

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::from_container(vec![1u8, 2, 3, 4]);
tensor.rotate_right(2);
assert_eq!(*tensor.get_element(0), 3);

Rotates the elements of the tensor to the left, inplace.

Example

use concrete_core::math::tensor::Tensor;
let mut tensor = Tensor::from_container(vec![1u8, 2, 3, 4]);
tensor.rotate_left(2);
assert_eq!(*tensor.get_element(0), 3);

Trait Implementations

The type of the elements of the collection

Returns a mutable slice from the container.

The element type.

The container used by the tensor.

Returns a mutable reference to the enclosed tensor.

The type of the elements of the collection.

Returns a slice from the container.

The element type.

The container used by the tensor.

Returns a reference to the enclosed tensor.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Creates a value from an iterator. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.