solve#
- class metatensor.solve(X: TensorMap, Y: TensorMap)[source]#
- Solve a linear system among two - TensorMap.- Solve the linear equation set - Y = X * wfor the unknown- w. Where- Y,- Xand- ware all- TensorMap.- Yand- Xmust have the same- keysand all their- TensorBlockmust be 2D-square array.- Parameters:
- Returns:
- a - TensorMapwith the same keys of- Yand- X, and where each- TensorBlockhas: the- sampleequal to the- propertiesof- Y; and the- propertiesequal to the- propertiesof- X.
- 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]]