Using SymPy (or another CAS) with Java - java

I am currently developing a project in JavaFX that accepts a mathematical expression and evaluates it. I have managed to get basic operations and a good deal of algebra to work. I have also managed to perform definite integrations using the trapezoid rule.
The main problem I am facing is with indefinite integration. I looked up many symbolic computation libraries online like JCalculus, JavaCalculus, JScience, SymJa and others. But none of them were as good as SymPy in terms of functionality. I wanted to know if there is a way to send commands to SymPy from Java and receive the result back into Java.
I am thinking of providing an installer for my JavaFX app that will install Python and SymPy if necessitated by the solution. I also tried to understand Jython, believing that it would somehow by related to the problem but I could not understand its purpose and its usage.
Please help me. I am quite good at Java and JavaFX but my knowledge of Python is severely low and restricted. Step by step instructions and sample code is extremely appreciated.
Thanks in advance.

If you somehow manage to write sympy equivalent python code from your java input in a file, you can get the output of that code very easily.
Process p = Runtime.getRuntime().exec("python yourfile.py");
Take a look at this.

Related

Convert compiled VB program to Java to use in Android app.

I have several VB programs that I wrote a few years ago in school. Is there any way possible to convert those programs to Java? Or would that it be easier to just rewrite it from scratch? My goal is to create an Android app that combines at least two of the programs into one functional app. This is purely a nonprofit endeavor; I'm a full time firefighter and am looking to put a free tool in the hands of my guys and other firemen who might want to use it.
I've been unable to locate the source code for the programs and have searched for an answer but haven't been able to find a definitive answer as most answers cover the source, not the compiled result. I've downloaded a couple supposed VB decompilers to see the results, but, in order to see the 'full' results, all the ones I've used require purchasing a 'pro' version. I have no problem paying for such a version, but I'd like to know if it's going to work properly before I do.
It would definitely be faster to rewrite them than it would be to devise a way of converting a VB program into Java code. Not only are the languages quite dissimilar, but VB's UI model is nothing like Android's, so it would likely be impossible (or at least impractical) to translate the UI code automatically.

How to graph a function based on a String on Android

This is a question I've asked myself since I began programming again and again.
My goal:
draw a function graph based on a string like "sin(x-1)*2.5" FAST.
fast means fast enough to be fun
it must run on Android
What I tried:
using my own very simple parser (slow and primitive)
using the JavaScript parser (slow but flexible)
using the Janino runtime compiler (fast and flexible, perfect, but only for normal Java)
I've googled extensively (queries like "android function graph how to"), but not found a solution,
although there are numerous applications managing to do this.
I will not accept "write your own parser/compiler" :)
SO: How do I do this?
Thank you very much in advance,
Till Höppner
Thanks to the help of harism and John Boker,
I achieved my goal quite easily.
My conclusions:
DO NOT put .CLASS libraries in your Android project, because these have .DEX files
Use either the dx tool, on those libraries, or use the source code of them
expr runs fastest in my app
Thank you, harism and John Boker
EDIT: This is nonsense, I don't know why I thought this...

Running algorithms in compiled C/C++ code within a Java/PHP/Python framework?

Occasionally, I have come across programming techniques that involve creating application frameworks or websites in Java, PHP or Python, but when complex algorithms are needed, writing those out in C or C++ and running them as API-like function calls within your Java/PHP/Python code.
I have been googling and searching around the net for this, and unless I don't know the name of the practice, I can't seem to find anything on it.
To put simply, how can I:
Create functions or classes in C or C++
Compile them into a DLL/binary/some form
Run the functions from -
Java
PHP
Python
I suspect JSON/XML like output and input must be created between the Java/PHP/Python and the C/C++ function so the data can be easily bridged, but that is okay.
I'm just not sure how to approach this technique, but it seems like a very smart way to take advantage of the great features of Java, PHP, and Python while at the same time utilizing the very fast programming languages for large, complex tasks.
The other thought going through my head is if I am creating functions using only literals in Java/PHP/Python, will it go nearly as fast as C anyway?
The specific tasks I'm looking to work with C/C++ on is massive loops, pinging a database, and analyzing maps. No work has started yet, its all theory now.
You can easily extend a python script with custom C++ code using Boost.Python, see this website for more details: http://www.boost.org/doc/libs/1_50_0/libs/python/doc/
This is how you can use it:
char const* greet()
{
return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
You need to compile this into a shared library. You will get a .dll on windows and a .so on Linux. The library will include the necessary code to make it available to python. Example using it:
>>> import hello_ext
>>> print hello_ext.greet()
hello, world
Here are some more examples: http://www.boost.org/doc/libs/1_50_0/libs/python/doc/tutorial/doc/html/index.html
When using Boost.Python remember to link your shared object to python if you are not using weak dynamic linking. There are similar things for PHP and Java.
As for other languages, I never used a custom shared library with Java but did so with PHP and it was a pain using the native Api. I found using swig way more pleasant.
Altough I agree with the comments (you might do it for fun, for business it's a bad idea) you might be interested in this similar question. The mentioned SWIG framework supports
all the languages you mentioned. I worked with it in a project with tons of legacy C code. Not really simple, but very powerful.
For Java, you can search JNI (Java Native Interface), there're a lot of guides telling how to use it.
On a slightly different take from the other proposed solutions, you could look into Gearman
Basically, it's a broker system. You have workers, which can be written in C in your case, to which you can delegate tasks from your python / php / java / w/e code.
Strong point is that you decouple both applications (if you rewrite your app in another language, you'll probably have less work as you only need to get the app to talk to Gearman).
Bad thing is that I think you'll be adding overhead which could make the performance boost irrelevant.

Easily exporting python to someone without the interpreter

Suppose I have a group of people to whom I want to show my python program. I don't want to have to tell them each "Ok, go to the python website, install the interpreter, open IDLE, open my program, press F5, and then it will run". Is there some way I can just send them a file of some format and they can run it easily?
I was thinking in the way of embedding it into Java, but the solutions I've seen for that seem to still require the python interpreter to run it.
P.S. I'm new to python, so I would be extremely grateful if responses aren't too technical and confusing. :)
I like cx-freeze for simplicity, but PyInstaller is also easy to use. Py2exe may be the traditional solution, but it hasn't been updated in 3 years.
One of the traditional solutions is: http://www.py2exe.org/

How to master java and ruby fastest for a PHPer?

I've been using PHP all the time.
Any advice to taking on these two languages?
I would say it really depends if you're used to the way OOP (object oriented programming) works. If you're not familiar with this way of thinking I would definately go with the book "Objects First With Java". It might look really, really basic at first, and you might be able to skip the first chapter or two. But if you read it from chapter 2 or 3 or so and finish it, you should have a good amount of knowledge to start building applications.
It's a little hard to help here because I don't know your level of skill when it comes to OOP. :) I've been writing PHP for a long time and didn't know a thing about OOP until I read the above-mentioned book.
All the best,
Bo
The same way you learned PHP - read the documentation, write some code, compile or execute it, debug it. Repeat until you are good. But don't expect to master a language quickly - anyone can learn to write code in a given language, but it takes time and effort to actually write good, high quality, and idiomatic code in that language.
The way I learn new languages is to read the documentation and other people's source code. It really helps to see what is possible in the language, without having it all wrapped up in academic speak.
Books are also helpful, if you have the time/patience to read through them.
A really good idea is to look up programs written in those languages and see if you can write the pseudo code for the programs. Then compare those to the source code and see what the difference is.
The best way to learn Ruby and/or Java is to forget the "PHP way" and to tackle each new language under their own idioms.
Both Ruby and Java have a fair selection of books (dead tree, electronic, free and non-free) as well as numerous free online tutorials. Ruby even has a nifty online interactive tutorial by _why (you did search didn't you?).
Learning the basic operation and syntax of each language is essential to avoid wasting time with random guessing as to why X doesn't work like Y. (Hint: If X doesn't work like Y, it's because X isn't Y.)
Trying to learn two languages at once is probably not the best idea. Ruby is quite similar to PHP, so the transition may be fairly simple, depending on your prior experience with other dynamic languages. You may find this site useful: http://railsforphp.com/
I recommend you try and build basic applications. Have a target, use the documentation and search the blogs or ask somebody if you're stuck. That's how I learned Ruby.
Also, for Ruby and Rails documentation I like APIdock, too bad they don't have Ruby 1.9 (which I recommend you use).
Click this: code-golf
Then solve all the challenges that got at least 10 upvotes in both Java and Ruby. Don't worry about the golf-scoring part, just do the best you can. If you post your efforts you may get some feedback, and you can compare your results with others.
Keep it enjoyable and simple at first. Use the learning style that works for YOU. If you like reading docs - great, otherwise you'll just end up with a nasty aftertaste. I'll say keep it enjoyable again because your initial exposure/experience can be greatly influenced by how you hit it off with a new language. Try to approach it from an angle of familiarity, you will find that there's some overlap between what you know and the new material. It will help if you can introduce the new stuff with as much ease as possible.
Recall what speaks to you or demonstrates most effectively when you learn and plot your course based on that. If you like books, find one that suites your style. Most of the books will give you most of the same information so what will make the most difference in a case like this is style of the book. For me "Java Objects" by J. Baker did the trick, my friend swears by "Thinking in Java". ... or find some screencasts if you like screencasts.
Then of course fire up the debugger and step though some code, but what ever you do first make sure you're enjoying it.
Start with Ruby. There is a wonderful online tutorial that lets you try Ruby right in your browsers. It covers the essentials of flow control and collections.
http://tryruby.sophrinix.com/
Java and PHP have more in common syntactically than Ruby and PHP. Sometimes that makes it harder instead of easier when learning a new language. That's why I think learning Ruby before Java will help. Ruby is also a lot easier to learn than Java, so the emotional return is greater and you'll be more equipped for Java.
As to learning Java, start with a Tutorial using Tapestry. Not so that you learn Tapestry, but so you gain the benefit of rapid development.
"Java developers love it because they can make Java code changes and see them immediately ... no redeploy, no restart!"
The down side to Tapestry is potential mess of setting up Tomcat.
I'd recommend getting yourself familiar an IDE (i.e. Eclipse) and working through some basic HelloWorld-esque problems. This will let you understand the lifecycle of a java program and some basic I/O. Maybe even take parts of a PHP project you've written and port it over to Java to get the basic syntax ideas down.

Categories

Resources