Search code examples
pythonarraysvariables

Filling a vector with a varying amount of variables


Hey currently I'm trying to program a quadratic programming algorithm with Python.

My goal: I want to program a function where the given parameters are one vector c, and a matrix G. They are connected through the function Phi = 0.5 *(x^T * G * x) + c^T *x (x^T in this context means vector x transposed). The goal of the function is to find a vector x so that the function Phi is minimized. In order to do that, I need to perform some algebraic calculations (multiplication, transposing and deriving the gradient of the function Phi).

My problem: But I'm struggling with creating a vector which indicates the dimensions of the problem. I am trying to create a vector x = [x_1, x_2, x_3, ..., x_N] which containts N elements. N varies. The elements 'x_N' should be variables (since I want to compute them later on, but I need them 'beforehand' to calculate e.g. the gradient).

My code so far: ('NoE'... is equal to N+1, thats why Im substracting 1 in my while statement)

 #filling the x-vector according to the dimension of the given problem
    temp_2 = 0
    while temp_2 <= (NoE-1):   
        x[temp_2]= 'x_' + temp_2
        print(x)

The previous answer just helped me partially: The only problem I am encountering now, is that those are all strings and I cant perform any kind of mathematical operation with them (like multiplying them with a matrix). Do you know any fix how I can still do it with strings?

Or do you think I could use the sympy library (which would help me with future calculations)?

Im open to every suggestion of solving this, since I dont have a lot of experience in programming generally

Thanks in advance!


Solution

  • Sounds like an x y problem, because I don't understand how you are going to initialize those variables. If you provide more context I will update my answer.

    If you want to have your list to contain variables or "symbols" on which you do algebraic operations, then the standard library doesn't have that, but you can use sympy:

    import sympy
    from sympy import symbols
    
    ### First we initialize some Symbol objects
    
    # this does essentially what you were doing in the question, but in one line
    # see list comprehensions* 
    symbol_names = [f'x_{i}' for i in range(NoE-1)]
    
    # then we make them become 'Symbols'
    x = symbols(symbol_names)
    
    ### Next we can do stuff with them:
    
    # multiply the first two
    expr = x[0] * x[1]
    print(expr)  # x_0*x_1
    
    # evaluate the expression `expr` with x_0=3, x_1=2
    res = expr.evalf(subs={'x_0':3, 'x_1':2})
    print(res)  # 6.00000
    

    Even though the sympy code above answer your question as it is, I don't think that it's the best solution to the problem you were describing.

    For example you could just have a list called x and then you populate it with elements

    x = []
    for i in range(NoE-1):
        x.append(
            float(input(f'insert value x_{i}'))
        )
    

    This way x will have all the inputted elements and you can access them via x[0], x[1], and so on.


    * docs.python.org/3/tutorial/datastructures.html#list-comprehensions