Take the tour ×

i am new to programming need to define operator/function s^m such that


a b c d g ..all symbols ^=power ,in simple language operator which will transform all 'g's in bracket to g^m while keeping those out of bracket as it is .

i am working on solving multivariate equations and for that i'm using cyclic function

**simplified version can be given as

equation 'n'=s^m(equation n-1)+a** and i need to get nth equation from the 1st equation i require s^m which will transform specific variable 'g' which is used many times in equation, to g^m

A possible clarification suggested by an astonished reader:

Need to define function transmogrify(strg) such that




... if "g" and "m" should be arguments to the function and can be more complicated than a single character, more clarification is needed.

share|improve this question
I can't even make a good guess as to what you are trying to ask. If you gave an example input or two and corresponding desired output and what you have tried already, you'd probably get assistance. –  msw Jul 15 '10 at 16:13
Are you trying to get Python to simplify your equations for you? Can you clarify your question a bit? That equation looks like a function of a whole lot of variables. –  nmichaels Jul 15 '10 at 16:17
Ouch. My eyes are hurting. –  Warren P Jul 15 '10 at 17:08
This is not an example of a good question. This is an example of a poor question. Even if English isn't your native tongue, communicating your ideas clearly and efficiently is just another facet of programming. If you can't clearly state what it is you want to do in English you have little hope of stating what you want to do in any programming language. –  Wayne Werner Jul 15 '10 at 17:08
Is this more of a math question than a Python question? If so, mathoverflow.net is the place. (If not, ignore me.) –  Paul D. Waite Jul 15 '10 at 17:37
show 1 more comment

closed as not a real question by msw, Jim Lewis, Philipp, Jim Garrison, Graviton Jul 16 '10 at 5:25


2 Answers

Using Sympy, a python symbolic maths package.

You will need to read the documentation if you want to expand on what I have written. Your question is difficult to understand, so this is the best I can do.

import sympy

a, b, c, d, f = sympy.symbols('abcdf')
g = sympy.Symbol('g')
m, n, o, p, q = sympy.symbols('mnopq')

eq1 = a**g**n + b**g**o + c**g**p + d**g**q

def s1(k, eqn):
    """This is a helper function to simplify 's'."""
    var1, exp1 = eqn.args
    var2, exp2 = exp1.args
    if var2 == g:
        return var1**var2**(exp2 + k)
    return eqn

def s(k, eqn):
    return sum(s1(k, part) for part in eqn.args)

print eq1 + a**g**f
print s(m, eq1) + a**g**f
share|improve this answer
add comment

Simplifying assumptions: All variables are a single lower-case letter. All operators are binary and are represented by a single non-letter. There are no constants.

  1. Parse the expression into a tree.

  2. Walk over the tree (depth first). Ignore any g at the "top level" ("in parentheses" requirement needs clarification). For each 'g' that's the left operand of '^', insert nodes to change g ^ right_operand to g ^ (right_operand + m). For each other g, replace it by nodes equivalent to g ^ m.

  3. Decompile the tree to a string.

share|improve this answer
add comment
