### Tensor tutorial: Operators and Modification

parent dec265fa
Pipeline #712 failed with stages
in 4 minutes and 12 seconds
doc/indices.md 0 → 100644
 # Indices and Equations ## Blockwise Construction of Tensors
 ... ... @@ -3,7 +3,7 @@ The basic building stone of this library and all Tensor Network methods is the Tensor, represented in the class xerus::Tensor. To simplify the work with these objects, xerus contains a number of helper functions that allow the quick creation and modification of sparse and dense tensors. In the following we will list the most important ones but advise you to also read the tutorials on [indices and equations]() and [decompositions]() to have the full toolset with which to work on individual the tutorials on [indices and equations](\ref md_indices) and [decompositions]() to have the full toolset with which to work on individual tensors. ## Creation of Tensors ... ... @@ -58,10 +58,10 @@ xerus::Tensor::dirac({2,2,2}, {1,1,1}); // a 4x4x4 tensor with i.i.d. Gaussian random values xerus::Tensor::random({4,4,4}); // a (4x4) x (4x4) random orthogonal operator drawn according to the Haar measure xerus::Tensor::random_orthogonal({4,4},{4,4}); // a 4x4x4 sparse tensor with 10 random entries in uniformly distributed random positions xerus::Tensor::random({4,4,4}, 10); // a (4x4) x (4x4) random orthogonal operator drawn according to the Haar measure xerus::Tensor::random_orthogonal({4,4},{4,4}); ~~~ ~~~.py # a 2x3x4 tensor with all entries = 1 ... ... @@ -76,10 +76,10 @@ xerus.Tensor.dirac([2,2,2], [1,1,1]) # a 4x4x4 tensor with i.i.d. Gaussian random values xerus.Tensor.random([4,4,4]) # a (4x4) x (4x4) random orthogonal operator drawn according to the Haar measure xerus.Tensor.random_orthogonal([4,4],[4,4]) # a 4x4x4 sparse tensor with 10 random entries in uniformly distributed random positions xerus.Tensor.random([4,4,4], n=10) # a (4x4) x (4x4) random orthogonal operator drawn according to the Haar measure xerus.Tensor.random_orthogonal([4,4],[4,4]) ~~~ If the entries of the tensor should be calculated externally, it is possible in c++ to either pass the raw data directly (as ... ... @@ -91,14 +91,14 @@ std::unique_ptr ptr = foo(); // NOTE: make sure that the dimensions are correct as there is no way for xerus to check this! F = xerus::Tensor({2,2,2}, ptr); // create a dense 2x2x2 tensor with every entry populated by a callback (lambda) function G = xerus::Tensor({2,2,2}, [](const std::vector &_idx) -> double { // create a dense 3x3x3 tensor with every entry populated by a callback (lambda) function G = xerus::Tensor({3,3,3}, [](const std::vector &_idx) -> double { // somehow derive the entry from the index positions in _idx double result = double( _idx * _idx * _idx ); return result; }); // create a sparse 16x16x16 tensor with 5 entries determined by a callback (lambda) function // create a sparse 16x16x16 tensor with 16 entries determined by a callback (lambda) function H = xerus::Tensor({16,16,16}, 16, [](size_t num, size_t max) -> std::pair { // insert number 1/5, 2/5, 3/5, 4/5, 5/5 // at positions 0 (= {0,0,0}), 17 (= {0,1,1}), 34 (= {0,2,2}) ... respectively ... ... @@ -106,8 +106,13 @@ H = xerus::Tensor({16,16,16}, 16, [](size_t num, size_t max) -> std::pair
 ... ... @@ -737,11 +737,11 @@ namespace xerus { /** * @brief Performs the trace over the given indices * @param _firstIndex the first index involved in the trace. * @param _secondIndex the second index involved in the trace. * @brief Performs the trace over the given modes * @param _firstMode the first mode involved in the trace. * @param _secondMode the second mode involved in the trace. */ void perform_trace(size_t _firstIndex, size_t _secondIndex); void perform_trace(size_t _firstMode, size_t _secondMode); /** ... ...
 ... ... @@ -463,12 +463,35 @@ BOOST_PYTHON_MODULE(xerus) { .def("sparsity", &Tensor::sparsity) .def("all_entries_valid", &Tensor::all_entries_valid) .def("reorder_cost", &Tensor::reorder_cost) .def("reinterpret_dimensions", &Tensor::reinterpret_dimensions) .def("reinterpret_dimensions", &Tensor::reinterpret_dimensions, arg("dim"), "Reinterprets the dimensions of the tensor." parametersDocstr "dim : list or tuple of int" ) .def("resize_mode", &Tensor::resize_mode, (arg("mode"), arg("newDimension"), arg("cutPosition")=~0ul) (arg("mode"), arg("newDim"), arg("cutPos")=~0ul), "Resizes a specific mode of the Tensor." parametersDocstr "mode : int\n" "newDim : int\n" "cutPos : int, optional (default: infinity)\n" " The position within the selected mode in front of which slates are inserted or removed." ) .def("fix_mode", &Tensor::fix_mode, (arg("mode"), arg("value")), "Fixes a specific mode to a specific value, effectively reducing the order by one." parametersDocstr "mode : int\n" "value : int" ) .def("remove_slate", &Tensor::remove_slate, ((arg("mode"), arg("pos")), "Removes a single slate from the Tensor, reducing dimension[mode] by one." parametersDocstr "mode : int\n" "pos : int" ) .def("fix_mode", &Tensor::fix_mode) .def("remove_slate", &Tensor::remove_slate) .def("perform_trace", &Tensor::perform_trace) .def("offset_add", &Tensor::offset_add) .def("use_dense_representation", &Tensor::use_dense_representation) ... ...
 ... ... @@ -768,18 +768,18 @@ namespace xerus { } void Tensor::perform_trace(size_t _firstIndex, size_t _secondIndex) { REQUIRE(_firstIndex != _secondIndex, "Given indices must not coincide"); REQUIRE(dimensions[_firstIndex] == dimensions[_secondIndex], "Dimensions of trace indices must coincide."); void Tensor::perform_trace(size_t _firstMode, size_t _secondMode) { REQUIRE(_firstMode != _secondMode, "Given indices must not coincide"); REQUIRE(dimensions[_firstMode] == dimensions[_secondMode], "Dimensions of trace indices must coincide."); if(_firstIndex > _secondIndex) { std::swap(_firstIndex, _secondIndex); } if(_firstMode > _secondMode) { std::swap(_firstMode, _secondMode); } const size_t front = misc::product(dimensions, 0, _firstIndex); const size_t mid = misc::product(dimensions, _firstIndex+1, _secondIndex); const size_t back = misc::product(dimensions, _secondIndex+1, degree()); const size_t traceDim = dimensions[_firstIndex]; const size_t front = misc::product(dimensions, 0, _firstMode); const size_t mid = misc::product(dimensions, _firstMode+1, _secondMode); const size_t back = misc::product(dimensions, _secondMode+1, degree()); const size_t traceDim = dimensions[_firstMode]; const size_t frontStepSize = traceDim*mid*traceDim*back; const size_t traceStepSize = mid*traceDim*back+back; const size_t midStepSize = traceDim*back; ... ... @@ -822,8 +822,8 @@ namespace xerus { sparseData.reset(newData.release()); } dimensions.erase(dimensions.begin()+_secondIndex); dimensions.erase(dimensions.begin()+_firstIndex); dimensions.erase(dimensions.begin()+_secondMode); dimensions.erase(dimensions.begin()+_firstMode); factor = 1.0; } ... ...
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment