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 unknown w. Where Y, X and w are all TensorMap. Y and X must have the same keys and all their TensorBlock must be 2D-square array.

Parameters:
Returns:

a TensorMap with the same keys of Y and X, and where each TensorBlock has: the sample equal to the properties of Y; and the properties equal to the properties of X.

Return type:

TensorMap

>>> 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]]