ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
linalg.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "core/linalg/AddMM.h"
9 #include "core/linalg/Det.h"
10 #include "core/linalg/Inverse.h"
11 #include "core/linalg/LU.h"
13 #include "core/linalg/Matmul.h"
14 #include "core/linalg/SVD.h"
15 #include "core/linalg/Solve.h"
16 #include "core/linalg/Tri.h"
18 #include "pybind/core/core.h"
19 #include "pybind/docstring.h"
20 
21 namespace cloudViewer {
22 namespace core {
23 
24 void pybind_core_linalg(py::module &m) {
25  m.def(
26  "matmul",
27  [](const Tensor &A, const Tensor &B) {
28  Tensor output;
29  Matmul(A, B, output);
30  return output;
31  },
32  "Function to perform matrix multiplication of two 2D tensors with "
33  "compatible shapes.",
34  "A"_a, "B"_a);
35  m.def(
36  "addmm",
37  [](const Tensor &input, const Tensor &A, const Tensor &B,
38  double alpha, double beta) {
39  Tensor output =
40  input.Expand({A.GetShape(0), B.GetShape(1)}).Clone();
41  AddMM(A, B, output, alpha, beta);
42  return output;
43  },
44  "Function to perform addmm of two 2D tensors with compatible "
45  "shapes. Specifically this function returns output = alpha * A @ B "
46  "+ beta * input.",
47  "input"_a, "A"_a, "B"_a, "alpha"_a, "beta"_a);
48  m.def(
49  "det",
50  [](Tensor &A) {
51  double det;
52  det = Det(A);
53  return det;
54  },
55  "Function to compute determinant of a 2D square tensor.", "A"_a);
56 
57  m.def(
58  "lu",
59  [](const Tensor &A, bool permute_l) {
60  Tensor permutation, lower, upper;
61  LU(A, permutation, lower, upper, permute_l);
62  return py::make_tuple(permutation, lower, upper);
63  },
64  "Function to compute LU factorisation of a square 2D tensor.",
65  "A"_a, "permute_l"_a = false);
66 
67  m.def(
68  "lu_ipiv",
69  [](const Tensor &A) {
70  Tensor ipiv, output;
71  LUIpiv(A, ipiv, output);
72  return py::make_tuple(ipiv, output);
73  },
74  "Function to compute LU factorisation of a square 2D tensor.",
75  "A"_a);
76 
77  m.def(
78  "inv",
79  [](const Tensor &A) {
80  Tensor output;
81  Inverse(A, output);
82  return output;
83  },
84  "Function to inverse a square 2D tensor.", "A"_a);
85 
86  m.def(
87  "solve",
88  [](const Tensor &A, const Tensor &B) {
89  Tensor output;
90  Solve(A, B, output);
91  return output;
92  },
93  "Function to solve X for a linear system AX = B where A is a "
94  "square "
95  "matrix",
96  "A"_a, "B"_a);
97 
98  m.def(
99  "lstsq",
100  [](const Tensor &A, const Tensor &B) {
101  Tensor output;
102  LeastSquares(A, B, output);
103  return output;
104  },
105  "Function to solve X for a linear system AX = B where A is a full "
106  "rank matrix.",
107  "A"_a, "B"_a);
108 
109  m.def(
110  "svd",
111  [](const Tensor &A) {
112  Tensor U, S, VT;
113  SVD(A, U, S, VT);
114  return py::make_tuple(U, S, VT);
115  },
116  "Function to decompose A with A = U S VT.", "A"_a);
117 
118  m.def(
119  "triu",
120  [](const Tensor &A, const int diagonal) {
121  Tensor U;
122  Triu(A, U, diagonal);
123  return U;
124  },
125  "Function to get upper triangular matrix, above diagonal", "A"_a,
126  "diagonal"_a = 0);
127 
128  m.def(
129  "tril",
130  [](const Tensor &A, const int diagonal) {
131  Tensor L;
132  Tril(A, L, diagonal);
133  return L;
134  },
135  "Function to get lower triangular matrix, below diagonal", "A"_a,
136  "diagonal"_a = 0);
137 
138  m.def(
139  "triul",
140  [](const Tensor &A, const int diagonal = 0) {
141  Tensor U, L;
142  Triul(A, U, L, diagonal);
143  return py::make_tuple(U, L);
144  },
145  "Function to get both upper and lower triangular matrix", "A"_a,
146  "diagonal"_a = 0);
147 }
148 
149 } // namespace core
150 } // namespace cloudViewer
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:638
SizeVector GetShape() const
Definition: Tensor.h:1127
void LeastSquares(const Tensor &A, const Tensor &B, Tensor &X)
Solve AX = B with QR decomposition. A is a full-rank m x n matrix (m >= n).
void Solve(const Tensor &A, const Tensor &B, Tensor &X)
Solve AX = B with LU decomposition. A is a square matrix.
Definition: Solve.cpp:22
void SVD(const Tensor &A, Tensor &U, Tensor &S, Tensor &VT)
Definition: SVD.cpp:17
void LUIpiv(const Tensor &A, Tensor &ipiv, Tensor &output)
Definition: LU.cpp:62
void AddMM(const Tensor &A, const Tensor &B, Tensor &output, double alpha, double beta)
Definition: AddMM.cpp:17
void Triu(const Tensor &A, Tensor &output, const int diagonal)
Definition: Tri.cpp:35
void Triul(const Tensor &A, Tensor &upper, Tensor &lower, const int diagonal)
Definition: Tri.cpp:79
void Inverse(const Tensor &A, Tensor &output)
Computes A^{-1} with LU factorization, where A is a N x N square matrix.
Definition: Inverse.cpp:18
void Tril(const Tensor &A, Tensor &output, const int diagonal)
Definition: Tri.cpp:57
void Matmul(const Tensor &A, const Tensor &B, Tensor &output)
Computes matrix multiplication C = AB.
Definition: Matmul.cpp:17
void LU(const Tensor &A, Tensor &permutation, Tensor &lower, Tensor &upper, const bool permute_l)
Definition: LU.cpp:126
void pybind_core_linalg(py::module &m)
Definition: linalg.cpp:24
double Det(const Tensor &A)
Definition: Det.cpp:16
Generic file read and write utility for python interface.