Search code examples
pythonmathcomputer-scienceheuristics

Estimating vector scalars to approach another vector


I have three vectors scaled by a, b and c respectively. How can I get an estimation of a, b and c so that the sum of the vectors is closest to a fourth vector? Here is an example


Solution

  • Use Numpy. The solve() function is what you are looking for.

    import numpy as np
    
    from numpy.linalg import solve
    
    A = np.array([[4,11,9],[6,5,7],[3,5,9]])
    B = np.array([3,8,5])
    X = solve(A,B)
    
    print(A)
    print(B)
    print(X)
    

    The output should be:

    [[ 4 11  9]
     [ 6  5  7]
     [ 3  5  9]]
    [3 8 5]
    [ 1.28723404 -0.54787234  0.43085106]