Skip to content

project_into_subspace

project_into_subspace(x, B, M=None)

Projects x into a subspace B via least squares. Solves the following optimization problem: argmin_z ||Bz - x||_2^2

Parameters:

Name Type Description Default
x (n, 1) float numpy array

Vector to be projected into the subspace

required
B (n, m) float numpy array

Subspace to project x into

required
M (m, m) float numpy array

Mass matrix defining the metric for projection. If None, set to identity matrix

None

Returns:

Name Type Description
z (m, 1) float numpy array

Projection of x into the subspace B

Source code in src\fast_cody\project_into_subspace.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def project_into_subspace(x, B, M=None):
    """ Projects x into a subspace B via least squares.
    Solves the following optimization problem:
        ```
            argmin_z ||Bz - x||_2^2
        ```
    Parameters
    ----------
    x : (n, 1) float numpy array
        Vector to be projected into the subspace
    B : (n, m) float numpy array
        Subspace to project x into
    M : (m, m) float numpy array
        Mass matrix defining the metric for projection. If None, set to identity matrix

    Returns
    -------
    z : (m, 1) float numpy array
        Projection of x into the subspace B
    """
    if M is None:
        M = sp.sparse.identity(x.shape[0])
    if M.shape[0] == B.shape[0]//3:
        M = sp.sparse.kron(sp.sparse.identity(3), M)
    BM = B.T @ M
    BMB = BM @ B;

    z = np.linalg.solve(BMB, BM @ x)
    return z