Solving multivariate equation for a subset of variables












4















I am using sympy to solve some equations and I am running into a problem. I have this issue with many equations but I will illustrate with an example. I have an equation with multiple variables and I want to solve this equation in terms of all variables but one is excluded. For instance the equation 0 = 2^n*(2-a) - b + 1. Here there are three variables a, b and n. I want to get the values for a and b not in terms of n so the a and b may not contain n.



2^n*(2-a) - b + 1 = 0

# Since we don't want to solve in terms of n we know that (2 - a)
# has to be zero and -b + 1 has to be zero.

2 - a = 0
a = 2

-b + 1 = 0
b = 1


I want sympy to do this. Maybe I'm just not looking at the right documentation but I have found no way to do this. When I use solve and instruct it to solve for symbols a and b sympy returns to me a single solution where a is defined in terms of n and b. I assume this means I am free to choose b and n, However I don't want to fix n to a specific value I want n to still be a variable.



Code:



import sympy

n = sympy.var("n", integer = True)
a = sympy.var("a")
b = sympy.var("b")
f = 2**n*(2-a) - b + 1

solutions = sympy.solve(f, [a,b], dict = True)
# this will return: "[{a: 2**(-n)*(2**(n + 1) - b + 1)}]".
# A single solution where b and n are free variables.
# However this means I have to choose an n I don't want
# to that I want it to hold for any n.


I really hope someone can help me. I have been searching google for hours now...










share|improve this question

























  • just to clarify, you want to solve this as if f is a polynomial over 2**n and it is equated to the zero polynomial?

    – Yakov Dan
    Jan 2 at 11:02













  • @YakovDan Not entirely. My example equation only has a single 2**n But more complex equation could in addition to that have other powers like 9^n.

    – John Smith
    Jan 2 at 11:13











  • ok, but still you want to find values for the coefficients that would always hold, regardless of n. So if your equations would have mixed powers like 2^n and 9^n, you could view this as a sum of two polynomials, one over 2^n and the other over 9^n

    – Yakov Dan
    Jan 2 at 11:16











  • @YakovDan Yes true!

    – John Smith
    Jan 2 at 12:06
















4















I am using sympy to solve some equations and I am running into a problem. I have this issue with many equations but I will illustrate with an example. I have an equation with multiple variables and I want to solve this equation in terms of all variables but one is excluded. For instance the equation 0 = 2^n*(2-a) - b + 1. Here there are three variables a, b and n. I want to get the values for a and b not in terms of n so the a and b may not contain n.



2^n*(2-a) - b + 1 = 0

# Since we don't want to solve in terms of n we know that (2 - a)
# has to be zero and -b + 1 has to be zero.

2 - a = 0
a = 2

-b + 1 = 0
b = 1


I want sympy to do this. Maybe I'm just not looking at the right documentation but I have found no way to do this. When I use solve and instruct it to solve for symbols a and b sympy returns to me a single solution where a is defined in terms of n and b. I assume this means I am free to choose b and n, However I don't want to fix n to a specific value I want n to still be a variable.



Code:



import sympy

n = sympy.var("n", integer = True)
a = sympy.var("a")
b = sympy.var("b")
f = 2**n*(2-a) - b + 1

solutions = sympy.solve(f, [a,b], dict = True)
# this will return: "[{a: 2**(-n)*(2**(n + 1) - b + 1)}]".
# A single solution where b and n are free variables.
# However this means I have to choose an n I don't want
# to that I want it to hold for any n.


I really hope someone can help me. I have been searching google for hours now...










share|improve this question

























  • just to clarify, you want to solve this as if f is a polynomial over 2**n and it is equated to the zero polynomial?

    – Yakov Dan
    Jan 2 at 11:02













  • @YakovDan Not entirely. My example equation only has a single 2**n But more complex equation could in addition to that have other powers like 9^n.

    – John Smith
    Jan 2 at 11:13











  • ok, but still you want to find values for the coefficients that would always hold, regardless of n. So if your equations would have mixed powers like 2^n and 9^n, you could view this as a sum of two polynomials, one over 2^n and the other over 9^n

    – Yakov Dan
    Jan 2 at 11:16











  • @YakovDan Yes true!

    – John Smith
    Jan 2 at 12:06














4












4








4


2






I am using sympy to solve some equations and I am running into a problem. I have this issue with many equations but I will illustrate with an example. I have an equation with multiple variables and I want to solve this equation in terms of all variables but one is excluded. For instance the equation 0 = 2^n*(2-a) - b + 1. Here there are three variables a, b and n. I want to get the values for a and b not in terms of n so the a and b may not contain n.



2^n*(2-a) - b + 1 = 0

# Since we don't want to solve in terms of n we know that (2 - a)
# has to be zero and -b + 1 has to be zero.

2 - a = 0
a = 2

-b + 1 = 0
b = 1


I want sympy to do this. Maybe I'm just not looking at the right documentation but I have found no way to do this. When I use solve and instruct it to solve for symbols a and b sympy returns to me a single solution where a is defined in terms of n and b. I assume this means I am free to choose b and n, However I don't want to fix n to a specific value I want n to still be a variable.



Code:



import sympy

n = sympy.var("n", integer = True)
a = sympy.var("a")
b = sympy.var("b")
f = 2**n*(2-a) - b + 1

solutions = sympy.solve(f, [a,b], dict = True)
# this will return: "[{a: 2**(-n)*(2**(n + 1) - b + 1)}]".
# A single solution where b and n are free variables.
# However this means I have to choose an n I don't want
# to that I want it to hold for any n.


I really hope someone can help me. I have been searching google for hours now...










share|improve this question
















I am using sympy to solve some equations and I am running into a problem. I have this issue with many equations but I will illustrate with an example. I have an equation with multiple variables and I want to solve this equation in terms of all variables but one is excluded. For instance the equation 0 = 2^n*(2-a) - b + 1. Here there are three variables a, b and n. I want to get the values for a and b not in terms of n so the a and b may not contain n.



2^n*(2-a) - b + 1 = 0

# Since we don't want to solve in terms of n we know that (2 - a)
# has to be zero and -b + 1 has to be zero.

2 - a = 0
a = 2

-b + 1 = 0
b = 1


I want sympy to do this. Maybe I'm just not looking at the right documentation but I have found no way to do this. When I use solve and instruct it to solve for symbols a and b sympy returns to me a single solution where a is defined in terms of n and b. I assume this means I am free to choose b and n, However I don't want to fix n to a specific value I want n to still be a variable.



Code:



import sympy

n = sympy.var("n", integer = True)
a = sympy.var("a")
b = sympy.var("b")
f = 2**n*(2-a) - b + 1

solutions = sympy.solve(f, [a,b], dict = True)
# this will return: "[{a: 2**(-n)*(2**(n + 1) - b + 1)}]".
# A single solution where b and n are free variables.
# However this means I have to choose an n I don't want
# to that I want it to hold for any n.


I really hope someone can help me. I have been searching google for hours now...







python python-3.x sympy equation-solving






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 9:57







John Smith

















asked Jan 2 at 8:41









John SmithJohn Smith

691622




691622













  • just to clarify, you want to solve this as if f is a polynomial over 2**n and it is equated to the zero polynomial?

    – Yakov Dan
    Jan 2 at 11:02













  • @YakovDan Not entirely. My example equation only has a single 2**n But more complex equation could in addition to that have other powers like 9^n.

    – John Smith
    Jan 2 at 11:13











  • ok, but still you want to find values for the coefficients that would always hold, regardless of n. So if your equations would have mixed powers like 2^n and 9^n, you could view this as a sum of two polynomials, one over 2^n and the other over 9^n

    – Yakov Dan
    Jan 2 at 11:16











  • @YakovDan Yes true!

    – John Smith
    Jan 2 at 12:06



















  • just to clarify, you want to solve this as if f is a polynomial over 2**n and it is equated to the zero polynomial?

    – Yakov Dan
    Jan 2 at 11:02













  • @YakovDan Not entirely. My example equation only has a single 2**n But more complex equation could in addition to that have other powers like 9^n.

    – John Smith
    Jan 2 at 11:13











  • ok, but still you want to find values for the coefficients that would always hold, regardless of n. So if your equations would have mixed powers like 2^n and 9^n, you could view this as a sum of two polynomials, one over 2^n and the other over 9^n

    – Yakov Dan
    Jan 2 at 11:16











  • @YakovDan Yes true!

    – John Smith
    Jan 2 at 12:06

















just to clarify, you want to solve this as if f is a polynomial over 2**n and it is equated to the zero polynomial?

– Yakov Dan
Jan 2 at 11:02







just to clarify, you want to solve this as if f is a polynomial over 2**n and it is equated to the zero polynomial?

– Yakov Dan
Jan 2 at 11:02















@YakovDan Not entirely. My example equation only has a single 2**n But more complex equation could in addition to that have other powers like 9^n.

– John Smith
Jan 2 at 11:13





@YakovDan Not entirely. My example equation only has a single 2**n But more complex equation could in addition to that have other powers like 9^n.

– John Smith
Jan 2 at 11:13













ok, but still you want to find values for the coefficients that would always hold, regardless of n. So if your equations would have mixed powers like 2^n and 9^n, you could view this as a sum of two polynomials, one over 2^n and the other over 9^n

– Yakov Dan
Jan 2 at 11:16





ok, but still you want to find values for the coefficients that would always hold, regardless of n. So if your equations would have mixed powers like 2^n and 9^n, you could view this as a sum of two polynomials, one over 2^n and the other over 9^n

– Yakov Dan
Jan 2 at 11:16













@YakovDan Yes true!

– John Smith
Jan 2 at 12:06





@YakovDan Yes true!

– John Smith
Jan 2 at 12:06












1 Answer
1






active

oldest

votes


















0














Ok, here's what I came up with. This seems to solve the type of equations you're looking for. I've provided some tests as well. Of course, this code is rough and can be easily caused to fail, so i'd take it more as a starting point than a complete solution



import sympy
n = sympy.Symbol('n')
a = sympy.Symbol('a')
b = sympy.Symbol('b')
c = sympy.Symbol('c')
d = sympy.Symbol('d')
e = sympy.Symbol('e')
f = sympy.sympify(2**n*(2-a) - b + 1)
g = sympy.sympify(2**n*(2-a) -2**(n-1)*(c+5) - b + 1)
h = sympy.sympify(2**n*(2-a) -2**(n-1)*(e-1) +(c-3)*9**n - b + 1)
i = sympy.sympify(2**n*(2-a) -2**(n-1)*(e+4) +(c-3)*9**n - b + 1 + (d+2)*9**(n+2))

def rewrite(expr):
if expr.is_Add:
return sympy.Add(*[rewrite(f) for f in expr.args])
if expr.is_Mul:
return sympy.Mul(*[rewrite(f) for f in expr.args])
if expr.is_Pow:
if expr.args[0].is_Number:
if expr.args[1].is_Symbol:
return expr
elif expr.args[1].is_Add:
base = expr.args[0]
power = sympy.solve(expr.args[1])
sym = expr.args[1].free_symbols.pop()
return sympy.Mul(sympy.Pow(base,-power[0]), sympy.Pow(base,sym))
else:
return expr

else:
return expr
else:
return expr




def my_solve(expr):

if not expr.is_Add:
return None

consts_list =
equations_list =
for arg in expr.args:
if not sympy.Symbol('n') in arg.free_symbols:
consts_list.append(arg)
elif arg.is_Mul:
coeff_list =
for nested_arg in arg.args:
if not sympy.Symbol('n') in nested_arg.free_symbols:
coeff_list.append(nested_arg)
equations_list.append(sympy.Mul(*coeff_list))

equations_list.append(sympy.Add(*consts_list))
results = {}
for eq in equations_list:
var_name = eq.free_symbols.pop()
val = sympy.solve(eq)[0]
results[var_name] = val



return results




print(my_solve(rewrite(f)))
print(my_solve(rewrite(g)))
print(my_solve(rewrite(h)))
print(my_solve(rewrite(i)))





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003390%2fsolving-multivariate-equation-for-a-subset-of-variables%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Ok, here's what I came up with. This seems to solve the type of equations you're looking for. I've provided some tests as well. Of course, this code is rough and can be easily caused to fail, so i'd take it more as a starting point than a complete solution



    import sympy
    n = sympy.Symbol('n')
    a = sympy.Symbol('a')
    b = sympy.Symbol('b')
    c = sympy.Symbol('c')
    d = sympy.Symbol('d')
    e = sympy.Symbol('e')
    f = sympy.sympify(2**n*(2-a) - b + 1)
    g = sympy.sympify(2**n*(2-a) -2**(n-1)*(c+5) - b + 1)
    h = sympy.sympify(2**n*(2-a) -2**(n-1)*(e-1) +(c-3)*9**n - b + 1)
    i = sympy.sympify(2**n*(2-a) -2**(n-1)*(e+4) +(c-3)*9**n - b + 1 + (d+2)*9**(n+2))

    def rewrite(expr):
    if expr.is_Add:
    return sympy.Add(*[rewrite(f) for f in expr.args])
    if expr.is_Mul:
    return sympy.Mul(*[rewrite(f) for f in expr.args])
    if expr.is_Pow:
    if expr.args[0].is_Number:
    if expr.args[1].is_Symbol:
    return expr
    elif expr.args[1].is_Add:
    base = expr.args[0]
    power = sympy.solve(expr.args[1])
    sym = expr.args[1].free_symbols.pop()
    return sympy.Mul(sympy.Pow(base,-power[0]), sympy.Pow(base,sym))
    else:
    return expr

    else:
    return expr
    else:
    return expr




    def my_solve(expr):

    if not expr.is_Add:
    return None

    consts_list =
    equations_list =
    for arg in expr.args:
    if not sympy.Symbol('n') in arg.free_symbols:
    consts_list.append(arg)
    elif arg.is_Mul:
    coeff_list =
    for nested_arg in arg.args:
    if not sympy.Symbol('n') in nested_arg.free_symbols:
    coeff_list.append(nested_arg)
    equations_list.append(sympy.Mul(*coeff_list))

    equations_list.append(sympy.Add(*consts_list))
    results = {}
    for eq in equations_list:
    var_name = eq.free_symbols.pop()
    val = sympy.solve(eq)[0]
    results[var_name] = val



    return results




    print(my_solve(rewrite(f)))
    print(my_solve(rewrite(g)))
    print(my_solve(rewrite(h)))
    print(my_solve(rewrite(i)))





    share|improve this answer




























      0














      Ok, here's what I came up with. This seems to solve the type of equations you're looking for. I've provided some tests as well. Of course, this code is rough and can be easily caused to fail, so i'd take it more as a starting point than a complete solution



      import sympy
      n = sympy.Symbol('n')
      a = sympy.Symbol('a')
      b = sympy.Symbol('b')
      c = sympy.Symbol('c')
      d = sympy.Symbol('d')
      e = sympy.Symbol('e')
      f = sympy.sympify(2**n*(2-a) - b + 1)
      g = sympy.sympify(2**n*(2-a) -2**(n-1)*(c+5) - b + 1)
      h = sympy.sympify(2**n*(2-a) -2**(n-1)*(e-1) +(c-3)*9**n - b + 1)
      i = sympy.sympify(2**n*(2-a) -2**(n-1)*(e+4) +(c-3)*9**n - b + 1 + (d+2)*9**(n+2))

      def rewrite(expr):
      if expr.is_Add:
      return sympy.Add(*[rewrite(f) for f in expr.args])
      if expr.is_Mul:
      return sympy.Mul(*[rewrite(f) for f in expr.args])
      if expr.is_Pow:
      if expr.args[0].is_Number:
      if expr.args[1].is_Symbol:
      return expr
      elif expr.args[1].is_Add:
      base = expr.args[0]
      power = sympy.solve(expr.args[1])
      sym = expr.args[1].free_symbols.pop()
      return sympy.Mul(sympy.Pow(base,-power[0]), sympy.Pow(base,sym))
      else:
      return expr

      else:
      return expr
      else:
      return expr




      def my_solve(expr):

      if not expr.is_Add:
      return None

      consts_list =
      equations_list =
      for arg in expr.args:
      if not sympy.Symbol('n') in arg.free_symbols:
      consts_list.append(arg)
      elif arg.is_Mul:
      coeff_list =
      for nested_arg in arg.args:
      if not sympy.Symbol('n') in nested_arg.free_symbols:
      coeff_list.append(nested_arg)
      equations_list.append(sympy.Mul(*coeff_list))

      equations_list.append(sympy.Add(*consts_list))
      results = {}
      for eq in equations_list:
      var_name = eq.free_symbols.pop()
      val = sympy.solve(eq)[0]
      results[var_name] = val



      return results




      print(my_solve(rewrite(f)))
      print(my_solve(rewrite(g)))
      print(my_solve(rewrite(h)))
      print(my_solve(rewrite(i)))





      share|improve this answer


























        0












        0








        0







        Ok, here's what I came up with. This seems to solve the type of equations you're looking for. I've provided some tests as well. Of course, this code is rough and can be easily caused to fail, so i'd take it more as a starting point than a complete solution



        import sympy
        n = sympy.Symbol('n')
        a = sympy.Symbol('a')
        b = sympy.Symbol('b')
        c = sympy.Symbol('c')
        d = sympy.Symbol('d')
        e = sympy.Symbol('e')
        f = sympy.sympify(2**n*(2-a) - b + 1)
        g = sympy.sympify(2**n*(2-a) -2**(n-1)*(c+5) - b + 1)
        h = sympy.sympify(2**n*(2-a) -2**(n-1)*(e-1) +(c-3)*9**n - b + 1)
        i = sympy.sympify(2**n*(2-a) -2**(n-1)*(e+4) +(c-3)*9**n - b + 1 + (d+2)*9**(n+2))

        def rewrite(expr):
        if expr.is_Add:
        return sympy.Add(*[rewrite(f) for f in expr.args])
        if expr.is_Mul:
        return sympy.Mul(*[rewrite(f) for f in expr.args])
        if expr.is_Pow:
        if expr.args[0].is_Number:
        if expr.args[1].is_Symbol:
        return expr
        elif expr.args[1].is_Add:
        base = expr.args[0]
        power = sympy.solve(expr.args[1])
        sym = expr.args[1].free_symbols.pop()
        return sympy.Mul(sympy.Pow(base,-power[0]), sympy.Pow(base,sym))
        else:
        return expr

        else:
        return expr
        else:
        return expr




        def my_solve(expr):

        if not expr.is_Add:
        return None

        consts_list =
        equations_list =
        for arg in expr.args:
        if not sympy.Symbol('n') in arg.free_symbols:
        consts_list.append(arg)
        elif arg.is_Mul:
        coeff_list =
        for nested_arg in arg.args:
        if not sympy.Symbol('n') in nested_arg.free_symbols:
        coeff_list.append(nested_arg)
        equations_list.append(sympy.Mul(*coeff_list))

        equations_list.append(sympy.Add(*consts_list))
        results = {}
        for eq in equations_list:
        var_name = eq.free_symbols.pop()
        val = sympy.solve(eq)[0]
        results[var_name] = val



        return results




        print(my_solve(rewrite(f)))
        print(my_solve(rewrite(g)))
        print(my_solve(rewrite(h)))
        print(my_solve(rewrite(i)))





        share|improve this answer













        Ok, here's what I came up with. This seems to solve the type of equations you're looking for. I've provided some tests as well. Of course, this code is rough and can be easily caused to fail, so i'd take it more as a starting point than a complete solution



        import sympy
        n = sympy.Symbol('n')
        a = sympy.Symbol('a')
        b = sympy.Symbol('b')
        c = sympy.Symbol('c')
        d = sympy.Symbol('d')
        e = sympy.Symbol('e')
        f = sympy.sympify(2**n*(2-a) - b + 1)
        g = sympy.sympify(2**n*(2-a) -2**(n-1)*(c+5) - b + 1)
        h = sympy.sympify(2**n*(2-a) -2**(n-1)*(e-1) +(c-3)*9**n - b + 1)
        i = sympy.sympify(2**n*(2-a) -2**(n-1)*(e+4) +(c-3)*9**n - b + 1 + (d+2)*9**(n+2))

        def rewrite(expr):
        if expr.is_Add:
        return sympy.Add(*[rewrite(f) for f in expr.args])
        if expr.is_Mul:
        return sympy.Mul(*[rewrite(f) for f in expr.args])
        if expr.is_Pow:
        if expr.args[0].is_Number:
        if expr.args[1].is_Symbol:
        return expr
        elif expr.args[1].is_Add:
        base = expr.args[0]
        power = sympy.solve(expr.args[1])
        sym = expr.args[1].free_symbols.pop()
        return sympy.Mul(sympy.Pow(base,-power[0]), sympy.Pow(base,sym))
        else:
        return expr

        else:
        return expr
        else:
        return expr




        def my_solve(expr):

        if not expr.is_Add:
        return None

        consts_list =
        equations_list =
        for arg in expr.args:
        if not sympy.Symbol('n') in arg.free_symbols:
        consts_list.append(arg)
        elif arg.is_Mul:
        coeff_list =
        for nested_arg in arg.args:
        if not sympy.Symbol('n') in nested_arg.free_symbols:
        coeff_list.append(nested_arg)
        equations_list.append(sympy.Mul(*coeff_list))

        equations_list.append(sympy.Add(*consts_list))
        results = {}
        for eq in equations_list:
        var_name = eq.free_symbols.pop()
        val = sympy.solve(eq)[0]
        results[var_name] = val



        return results




        print(my_solve(rewrite(f)))
        print(my_solve(rewrite(g)))
        print(my_solve(rewrite(h)))
        print(my_solve(rewrite(i)))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 6 at 12:52









        Yakov DanYakov Dan

        1,401718




        1,401718
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003390%2fsolving-multivariate-equation-for-a-subset-of-variables%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules