Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6040211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:28:51+00:00 2026-05-23T06:28:51+00:00

i want to model an equation as a tree in python. for example: x

  • 0

i want to model an equation as a tree in python.
for example:

x = exp(((-0.5)*((f/sqrt(d))**2)))

how do i do this? i want to be able to switch tree branches,
delete parts of the tree etc, and then convert it back into
a new equation in text form.

can you give me example code/ Libraries that can do this?

cheers
edit 1:

I have come this far now:

import compiler
import pprint
import parser
import ast
class Py2Neko(ast.NodeTransformer):
    def generic_visit(self, node):
        print type(node).__name__
        ast.NodeVisitor.generic_visit(self, node)

    def visit_Name(self, node):
        print 'Name :', node.id

    def visit_Num(self, node):
        print 'Num :', node.__dict__['n']

    def visit_Str(self, node):
        print "Str :", node.s

    def visit_Print(self, node):
        print "Print :"
        ast.NodeVisitor.generic_visit(self, node)

    def visit_Assign(self, node):
        print "Assign :"
        ast.NodeVisitor.generic_visit(self, node)

    def visit_Expr(self, node):
        print "Expr :"
        ast.NodeVisitor.generic_visit(self, node)

if __name__ == '__main__':
    node = ast.parse("res= exp(((-0.5*one)*((delta_w*one/delta*one)**2)))")

    # print ast.dump(node)
    v = Py2Neko()
    v.visit(node)

Now it prints all nodes of the tree. However i want to be able to switch branches,
delete branches, insert branches and change operators/operands.
I need this because i want to be able to mutate the tree randomly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:28:52+00:00Added an answer on May 23, 2026 at 6:28 am

    Operators and functions are parent nodes, and operands are the leaves.

    x = exp(((-0.5)*((f/sqrt(d))**2)))
    

    Start with a top-down approach: [ operator { operand1, operand2 }]

    [ = { x, exp(((-0.5)*((f/sqrt(d))**2))) }]
    

    followed by:

    [ = { x, [ * { exp(((-0.5), ((f/sqrt(d))**2))) }] }]
    

    then:

    [ = { x, [ * { [ exp { -0.5 }],  [ ** { f/sqrt(d)), 2 }] }] }]
    

    You get the idea.

    This link might be your answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a model named Model. I'm pretty sure this isn't allowed,
I want to model the (biological) cell division process. I have been able to
I want to model multithreading app behavior. There's lot's of synchronization (ManualResetEvents etc.). I
I want to be able to store an atomic model in an OpenGL program
I want model the flight of a couple of planes using path data. I
I want to model a association between two classes in UML, that says an
at first sorry my bad English I want load model in my plugin controller
I want a Django model to have a date field in which the year,
I want to turn Model.attribute into Attribute.models. e.g. I currently have Model.first.attribute => string
I want to filter my model object using two filters. So, it can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.