Commit 4112646f authored by Ben Huber's avatar Ben Huber

Tensor tutorial: Operators and Modification

parent dec265fa
Pipeline #712 failed with stages
in 4 minutes and 12 seconds
# Indices and Equations
## Blockwise Construction of Tensors
This diff is collapsed.
......@@ -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;
}
......
Markdown is supported
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