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 6972833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:01:22+00:00 2026-05-27T17:01:22+00:00

I am in the somewhat unfortunate position to try to convert a program from

  • 0

I am in the somewhat unfortunate position to try to convert a program from the depths of CERN ROOT to python. In ROOT code (CINT in itself is an abomination imo), one can store mathematical functions as a “string” and pass these along to ROOT for fitting, plotting, etc. because of how ROOT defines these as “strings.”

At the moment, the mathematical functions are stored in simple text files as a line, i.e.

(1+[1])^(1+[1])/TMath::Gamma(1+[1]) * x^[1]/[0]^(1+[1]) * exp(-(1+[1])*x/[0])

and are then extracted as strings by C++ when reading in the file. Is there something similar in python? I know of numexpr, but I cant seem to get it to work with the equivalent of the above, i.e.

(1+p[1])**(1+p[1])/scipy.special.Gamma(1+p[1]) * x**p[1]/p[0]**(1+p[1]) * numpy.exp(-(1+p[1])*x/p[0])

Thanks a bunch in advance.

  • 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-27T17:01:23+00:00Added an answer on May 27, 2026 at 5:01 pm

    Since, presumably, you can trust the strings to be non-malicious, you could build a string which defines a function which evaluates the expression and use exec to execute that string as a statement. For example,

    import numpy as np
    import scipy.special as special
    
    expr='(1+p[1])**(1+p[1])/special.gamma(1+p[1]) * x**p[1]/p[0]**(1+p[1]) * np.exp(-(1+p[1])*x/p[0])'
    
    def make_func(expr):
        funcstr='''\
    def f(x,p):
        return {e}
        '''.format(e=expr)
        exec(funcstr)
        return f
    
    f=make_func(expr)
    print(f(1,[2,3]))
    

    returns

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

Sidebar

Related Questions

Somewhat to my surprise, the following code prints out Close twice. Running through the
I'm currently somewhat stuck getting a regular expression in Perl (taken from an earlier
For reasons which are somewhat unavoidable (lots of legacy code, compatibility, design needs) I
Somewhat unclear to me are references (pointers?) to classes in VB.NET. The question I
Somewhat related to my question about integers instead of decimals; my vendor provides a
Somewhat related to this question , but in the absence of any answer about
Somewhat similar to this question , except we haven't decided that we're going with
Although somewhat related to this question , I have what I think is a
Being somewhat typographically obsessed, I am curious: what is the best way to present
This is a somewhat low-level question. In x86 assembly there are two SSE instructions:

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.