| 4852 | |
| 4853 | |
| 4854 | shared_ptr<CoefficientFunction> |
| 4855 | MakeTensorTransposeCoefficientFunction (shared_ptr<CoefficientFunction> c1, Array<int> ordering) |
| 4856 | { |
| 4857 | auto dims1 = c1->Dimensions(); |
| 4858 | if (dims1.Size() != ordering.Size()) |
| 4859 | throw Exception("TensorTranspose - tensor dimensions don't match"); |
| 4860 | |
| 4861 | if (!c1->IsVariable()) |
| 4862 | if (auto subcf = dynamic_pointer_cast<SubTensorCoefficientFunction>(c1)) |
| 4863 | { |
| 4864 | cout << IM(2) << "Optimization: Tensor-Transpose of subtensor is a subtensor" << endl; |
| 4865 | Array<int> dims(dims1.Size()), dist(dims1.Size()); |
| 4866 | for (int i = 0; i < dims.Size(); i++) |
| 4867 | { |
| 4868 | if (ordering[i] < 0 || ordering[i] >= dims1.Size()) |
| 4869 | throw Exception ("ordering out of range"); |
| 4870 | dims[i] = dims1[ordering[i]]; |
| 4871 | dist[i] = subcf->Dist()[ordering[i]]; |
| 4872 | } |
| 4873 | |
| 4874 | auto input = c1->InputCoefficientFunctions(); |
| 4875 | auto res = MakeSubTensorCoefficientFunction (input[0], subcf->First(), |
| 4876 | std::move(dims), std::move(dist)); |
| 4877 | return res; |
| 4878 | } |
| 4879 | |
| 4880 | Array<int> dist1(dims1.Size()); |
| 4881 | |
| 4882 | int disti = 1; |
| 4883 | for (int i = dims1.Size()-1; i >= 0; i--) |
| 4884 | { |
| 4885 | dist1[i] = disti; |
| 4886 | disti *= dims1[i]; |
| 4887 | } |
| 4888 | |
| 4889 | Array<int> dims(dims1.Size()), dist(dims1.Size()); |
| 4890 | for (int i = 0; i < dims.Size(); i++) |
| 4891 | { |
| 4892 | if (ordering[i] < 0 || ordering[i] >= dims1.Size()) |
| 4893 | throw Exception ("ordering out of range"); |
| 4894 | dims[i] = dims1[ordering[i]]; |
| 4895 | dist[i] = dist1[ordering[i]]; |
| 4896 | } |
| 4897 | |
| 4898 | auto res = MakeSubTensorCoefficientFunction (c1, 0, std::move(dims), std::move(dist)); |
| 4899 | stringstream descr; |
| 4900 | descr << "tensor-transpose ["; |
| 4901 | for (auto i : Range(ordering.Size() - 1)) |
| 4902 | descr << " " << ordering[i] << ","; |
| 4903 | descr << " " << ordering.Last() << " ]"; |
| 4904 | res -> SetDescription(descr.str()); |
| 4905 | return res; |
| 4906 | } |
| 4907 | |
| 4908 | shared_ptr<CoefficientFunction> |
| 4909 | MakeTensorTransposeCoefficientFunction (shared_ptr<CoefficientFunction> c1, int i1, int i2) |
no test coverage detected