skip to Main Content

I have used Ubuntu to open a Python project, via VSC, in order to solve for multiple variables in a linear algebra scenario. To show the code:

from sympy.solvers import solve
from sympy import Symbol
x = Symbol('x')

solve[(.85*x - 17.59, x)

I end up solving for X. However, I have an array:

array([[17.594],
       [15.79 ],
       [11.31 ],
       [16.39 ],
       [13.87 ],
       [16.99 ]])

I would like to use the following array and solve:

.85x - ([[17.594],   = 0
.85x =   [15.79 ],   = 0
.85x -   [11.31 ],   = 0
.85x -   [16.39 ],   = 0
.85x -   [13.87 ],   = 0
.85x -   [16.99 ]])  = 0

I am attempting to subtract the array from .85x while solving for x for each individual number in the array. I do not want to do this individually for each number; I want to be able to solve for each value of x all at once (I am going to have 1000s of numbers in the array).

I have been trying for hours to find a way to do this with no luck. Some help would be greatly appreciated.

3

Answers


  1. Correct me if I’m wrong, but if that’s a NumPy array, it’s easier to solve this with simple algebra:

    .85x - n = 0    where n is an element of the array
    .85x = n
    x = n / .85
    
    >>> x = arr / .85
    >>> x
    array([[20.69882353],
           [18.57647059],
           [13.30588235],
           [19.28235294],
           [16.31764706],
           [19.98823529]])
    
    Login or Signup to reply.
  2. It can be done.

    In [108]: import sympy as sy
         ...: x = sy.Matrix(sy.symbols('x1 x2 x3 x4 x5 x6'))
         ...: c = sy.Matrix([17594, 15790, 11310, 16390, 13870, 16990])/1000
         ...: sy.solve(x*85/100-c, x)
    Out[108]: {x1: 8797/425, x2: 1579/85, x3: 1131/85, x4: 1639/85, x5: 1387/85, x6: 1699/85}
    

    Is this what you really want? Isn’t it easier with Numpy, as in wjandrea‘s answer?

    Login or Signup to reply.
  3. You can use a list comprehension to solve the value of for each of the element in the array.

    Array =[17.594, 15.79, 11.31, 16.39,13.87, 16.99 ]
    [solve(.85*x - i, x) for i in Array]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search