2

I have a (hypothetical) question and I think the solution would be to dynamically generate code.

I want to quickly evaluate an arbitrary mathematical function that a user has entered, say to find the sum i=1 to N of i^3+2i^2+6i+1. N is arbitrary and i^3+2i^2+6i+1 is arbitrary too (it need not be a polynomial, and it might contain trigonometric functions and other functions too). Suppose N can be very large. I want to know how I can evaluate the answer quickly, assuming that I have already parsed the user input to some bytecode or something else my program can understand.

If possible, I would also like my code to be easily compiled and run on different operating systems (including mobile).

I have thought of a few ways:

1) Write an interpreter that interprets and executes each command in my bytecode. This makes me free to use any language, but it's slow.

2) Write in Java/C# and use the dynamic code generation (e.g. Is it possible to dynamically compile and execute C# code fragments?). This would execute as as fast as if I had written the function directly in my source code, with a only a slight slowdown as C#/Java are both JIT-compiled to machine code. The limitation is that Java isn't widely supported on mobile, and C# is Windows-only.

3) Embed an assembler/C++ compiler/compiler for whatever compiled language that I use. The limitation is that it won't work on mobile either - it won't let me execute a data file.

4) Write HTML/Javascript then embed it in a web browser control and put it in an application (I think this is the way some people use to make a universal app that would run anywhere). But it's slow too and writing real applications in Javascript is a pain.

Which option do you think is most suitable? Or perhaps I should go with a mix, maybe my application code will create and execute a generated Javascript function?

Community
  • 1
  • 1
Bernard
  • 5,209
  • 1
  • 34
  • 64
  • Why is this tagged javascript? Do you want to write the bytecode interpreter or Java/C# compiler in javascript? What is your actual environment - does your app execute in the browser or as an OS executable? Do you actually want to generate readable code in a (what?) language of your choice to output to the user, or is the only aim to evaluate that mathematical sum expression? – Bergi Sep 04 '14 at 14:53
  • Simple solution: [Reach out to the network](https://www.wolframalpha.com/input/?i=sum+i^3%2B2i^2%2B6i%2B1+over+i=1+to+N) :-) – Bergi Sep 04 '14 at 14:56
  • Thanks for the comment; I realized I wasn't clear. My original intention was to write a mobile app, so I can embed a web browser control in the mobile app, and get it to display the page. I'm extending the question to desktop as well to see if there are any better solutions. The only aim is to get the numerical answer as fast as possible. – Bernard Sep 05 '14 at 01:26
  • Wolfram Alpha always works, but this is a project that I intend to do in my free time for fun, so I'd prefer to do the calculations myself (: – Bernard Sep 05 '14 at 01:28
  • Is this for client-side JavaScript / i.e. a webapp ? – Wylie Kulik Sep 08 '15 at 08:39
  • I was thinking of desktop programs and mobile apps that one can download from an app store, and the app shouldn't need an internet connection to work. The JavaScript that I mentioned in the question would likely be embedded in an app so I'm not limited to just using things that would work in a browser. – Bernard Sep 08 '15 at 12:04

1 Answers1

0

The fastest and simplest way to perform these calculations on large values of N are with raw maths instead of repeated summation.

Here's a formula to calculate each individual item in the expression, perform this for all items in the expression and you are done:

sum x = 1 to N for x^k

H[n] is the nth Harmonic number.

There are multiple approaches to calculating H[n]. Some calculate the largest required number and generate all up to that number, saving any other values required... Alternately store every 10,000th item in the series in a file and calculate H[n] from the nearest entry.

  • This works, but it's only limited to polynomials. I was hoping for something that would work for any mathematical expression (anything a scientific calculator would be able to do). I'm sorry I didn't state it explicitly in my question; I'll edit it. – Bernard Sep 05 '14 at 01:38
  • 1
    Arbitrary expressions like a scientific calculator? I don't think you want to write your own [CAS](https://en.wikipedia.org/wiki/Computer_algebra_system), rather try to find a software that you can embed in your app. – Bergi Sep 05 '14 at 12:11
  • Hmm I've not considered a CAS but that's an interesting idea to consider. When I mentioned a scientific calculator, I meant a "dumb" calculator that evaluated expressions numerically (it doesn't do algebra) so I didn't want to look at "smarter" algorithms that does algebraic analysis. But I think your suggestion is interesting, so I'll upvote! – Bernard Sep 05 '14 at 14:35