solve#
- class metatensor.solve(X: TensorMap, Y: TensorMap)[source]#
Solve a linear system among two
TensorMap
.Solve the linear equation set
Y = X * w
for the unknownw
. WhereY
,X
andw
are allTensorMap
.Y
andX
must have the samekeys
and all theirTensorBlock
must be 2D-square array.- Parameters:
- Returns:
a
TensorMap
with the same keys ofY
andX
, and where eachTensorBlock
has: thesample
equal to theproperties
ofY
; and theproperties
equal to theproperties
ofX
.- Return type:
>>> import numpy as np >>> import metatensor >>> from metatensor import TensorBlock, TensorMap, Labels >>> np.random.seed(0) >>> # We construct two independent variables, each sampled at 100 random points >>> X_values = np.random.rand(100, 2) >>> true_c = np.array([10.0, 42.0]) >>> # Build a linear function of the two variables, with coefficients defined >>> # in the true_c array, and add some random noise >>> y_values = (X_values @ true_c + np.random.normal(size=(100,))).reshape((100, 1)) >>> covariance = X_values.T @ X_values >>> y_regression = X_values.T @ y_values >>> X = TensorMap( ... keys=Labels(names=["dummy"], values=np.array([[0]])), ... blocks=[ ... TensorBlock( ... samples=Labels.range("sample", 2), ... components=[], ... properties=Labels.range("properties_for_regression", 2), ... values=covariance, ... ) ... ], ... ) >>> y = TensorMap( ... keys=Labels(names=["dummy"], values=np.array([[0]])), ... blocks=[ ... TensorBlock( ... samples=Labels.range("sample", 2), ... components=[], ... properties=Labels.range("property_to_regress", 1), ... values=y_regression, ... ) ... ], ... ) >>> c = metatensor.solve(X, y) >>> print(c.block()) TensorBlock samples (1): ['property_to_regress'] components (): [] properties (2): ['properties_for_regression'] gradients: None >>> # c should now be close to true_c >>> print(c.block().values) [[ 9.67680334 42.12534656]]