Which programming language should I use?

In summary, Fortran is a language that is good for scientific calculations, but is not the best for modern programming. Java is a modern language that is good for general purpose programming, but is also very versatile. C++ is a modern language that is good for complex calculations, but is also very versatile. VPython is a language that is new and is good for complex calculations.
  • #1
yup790
21
0
I was wondering which programming language I should learn for general purpose calculations and physics simulations.

I already know a bit java.
 
Technology news on Phys.org
  • #2
I suppose it would depend upon what compilers were available on the computer you intend to use. Fortran is the old standby, but there are others out there that are probably just as effective.
 
  • #3
That seems to be a bit of a theological question around here (and probably everywhere). Different folks will SWEAR to you that only <insert preferred language name> is the right one. Java will do you fine, as will others. FORTRAN, as old and creaky as it is, has TONS of subroutines that have accumulate over the years specifically for scientific applications so that's in its favor. If you plan on doing much in modern computing, especially outside of science programming, I recommend that whatever you pick, it be fully OO like Java or C++ or C# or VB.NET
 
  • #4
yup790 said:
I was wondering which programming language I should learn for general purpose calculations and physics simulations.

I already know a bit java.

If you need speed, then anything where you can compile to an optimum representation like the machine code for your desired platform.

You might even have to learn vector based CPU instructions, if speed is super super critical. An example of SIMD instructions are the SSE and SSE2 instruction sets for common x86-type platforms.
 
  • #5
Fortran 77. Is there another language yet that directly handles complex variables?

For 'general' use (implying graphs, diagrams and 'free') I was planning to take a good look at the current state of VPython sometime. Has anyone any experience with VPython?
 
  • #6
phinds said:
FORTRAN, as old and creaky as it is, has TONS of subroutines that have accumulate over the years specifically for scientific applications so that's in its favor. If you plan on doing much in modern computing, especially outside of science programming, I recommend that whatever you pick, it be fully OO like Java or C++ or C# or VB.NET

I just want to point out that while Olde FORTRAN (77 and earlier) is indeed old and creaky (though still very useful), modern Fortran (90 and later) is comparable to the other languages mentioned, and is fully OO now. And as of the latest 2008 standard, it's the only language with built-in parallel processing support in the form of coarrays, if you're planning on doing expensive calculations. I'm just saying, it's not your grandpa's FORTRAN anymore, though it's still more physicist-friendly than others.

But if I were you, I'd just stick with Java if you already know it well. I know that the big particle-in-cell codes are mostly written in C++ or Fortran, but Java is starting to show up more and more, and they all have comparable speeds. These days it doesn't matter as much. It really comes down to using what you're most comfortable with.
 
  • #7
PICsmith said:
I just want to point out that while Olde FORTRAN (77 and earlier) is indeed old and creaky (though still very useful), modern Fortran (90 and later) is comparable to the other languages mentioned, and is fully OO now.

Thanks for that update. I'm pretty clear now that *I* am what's old and creaky in my post ... I have not kept up w/ FORTRAN so was not aware it had made such great strides (FORTRAN 77 is very NEW by my standards --- I stopped programming in FORTRAN prior to that).

I would hazard a guess however that if it is indeed fully OO it really doesn't much resemble the original FORTRAN at all so I pretty clearly need to just stop thinking of it as FORTRAN at all
 
  • #8
cmb said:
Fortran 77. Is there another language yet that directly handles complex variables?

C++ does.

Code:
#include <iostream>
#include <complex>

using namespace std;

int main ()
{
    complex<double> z1, z2, z3;
    z1 = complex<double> (1.0, 2.0);
    z2 = complex<double> (3.0, 4.0);
    cout << "z1 = " << z1 << endl;
    cout << "z2 = " << z2 << endl;
    z3 = z1 + z2;
    cout << "Sum = " << z3 << endl;
    z3 = z1 * z2;
    cout << "Product = " << z3 << endl;
    return 0;
}

PICsmith said:
But if I were you, I'd just stick with Java if you already know it well.

Even if you don't already know it well, but have made a fair start on it, I'd stick with it and get lots of practice programming in it before moving on to another language. Good program design, coding and debugging techniques are applicable in most languages. If you know how to "program well" in general, you can easily pick up a new language when necessary, and apply those general techniques.
 
Last edited:
  • #9
OK, so what does C++ return when you enter cos-¹(2)?

F77 returns a complex value.
 
  • #10
No go... the C++ standard library doesn't seem to include a version of acos() for complex numbers. It does include automatically-overloaded complex versions of cos(), cosh(), exp(), log(), log10(), pow(), sin(), sinh(), sqrt(), tan(), tanh(). (I tried cos().)

However, you can write your own complex inverse trig functions using the complex logarithmic forms:

http://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Logarithmic_forms

Code:
#include <iostream>
#include <complex>
#include <cmath>

using namespace std;

// my attempt at a complex-valued acos() function

complex<double> acos (const complex<double>& x)
{
    const complex<double> eye = complex<double>(0,1);
    const complex<double> one = 1;
    return -eye*log(x + eye*sqrt(one - x*x));
}

int main ()
{
    complex<double> z = 2;
    cout << acos(z) << endl;  // this outputs "(0,1.31696)"
    return 0;
}

Of course a built-in library function might use a different method which is more accurate or efficient.
 
Last edited:
  • #11
jtbell said:
C++ does.

Yep, and with it's operator and function overloading it's surprising how well this added class feels like a language inbuilt feature. Also I've found it's precision is actually better than the Fortran built in type (at least for the only two freeware F95 implementations that I've found). I'm not sure why, but both the gnu "g95" fortran95 compiler and the Silverfrost ftn95 have serious accuracy problems with double precision complex numbers. No issues to report with the gcc c++ compilers implementation of double precision complex however.

I'd hope that a commercial compiler like ifort can handle double precision complex properly, but I've never had the opportunity to test it.

I've got to admit Fortran 90 (and onward) has come a long way, and it's probable still a contender for number crunching programs. I'm not really fan of Fortran as a general purpose language though. It still carries some legacy stuff that I hate. I don't like implicit typing (or having to "implicit none" for every function) and I really hate Fortran I/O.
 
Last edited:
  • #12
uart said:
I really hate Fortran I/O.

Once you learn to pipe data outputs into text streams and such [much less chance of crashing mid-analysis], then post-process the data, it all makes much more sense to treat F77 as a computer-based 'engineer's calculator'.

If you want (/need) flash and bling, buy MatLab.
 
  • #13
cmb said:
If you want (/need) flash and bling, buy MatLab.

Or get one of the freeware clones like Octave.

Actually that is a bit of a plus for Fortran, it's close syntax similarity to MATLAB is very handy.
 
Last edited:
  • #14
cmb said:
OK, so what does C++ return when you enter cos-¹(2)?

F77 returns a complex value.

That's well spotted. I'd never noticed that the C++ complex implementation didn't include any inverse trig functions. Yeah I agree that having as complete as possible function coverage is a very important feature for a complex type. It makes it so much easier, especially when you're translating an algorithm, originally written for reals, to handle complex. With a good inbuilt complex type and complete function coverage it can often make the job almost trivial.

I'm surprised that they didn't include inverse trig's in the c++ complex implementation, as jtbell pointed out above it's not very difficult. I just wrote the following inverse trig library in about 15 minutes if anyone wants it. I even threw in the hyp-trig's at no extra cost. :smile:

Code:
/* File complex_itrig.h */
/* Uart's complex inverse trig library */

#include <complex>
typedef std::complex<double> cmplx;
const cmplx _I = cmplx(0.0,1.0);
const cmplx _1 = cmplx(1.0,0.0);

cmplx atan(cmplx x) {
       return 0.5*_I*( log(_1 - _I*x) - log(_1 + _I*x) );
       }

cmplx asin(cmplx x) {
      return atan( x / sqrt(_1-x*x) );
      }
             
cmplx acos(cmplx x) {
      return atan( sqrt(_1-x*x) / x );
      }

cmplx acosh(cmplx x) {
      return -_I * acos(x);
      }

cmplx asinh(cmplx x) {
      return -_I * asin(_I*x);
      }

cmplx atanh(cmplx x) {
      return -_I * atan(_I*x);
      }
 
  • #15
cmb said:
For 'general' use (implying graphs, diagrams and 'free') I was planning to take a good look at the current state of VPython sometime. Has anyone any experience with VPython?

For taking an idea for an interactive 3D simulation/visualization [for presenting to my class]
and quickly getting it somewhat working, I use VPython.
 
  • #16
Hi...i'm new to ths blog...
Plz tel me, homework s Fortran(Gfortran95) language?
 
  • #17
I'll advocate Python (good enough for general purpose, and fairly simple to learn) and its scientific libraries (for physics simulations, as you mentioned, although this term is a bit broad but you can definitely do a lot with Python).
 
  • #18
Manju K said:
Hi...i'm new to ths blog...
Plz tel me, homework s Fortran(Gfortran95) language?

(1) This is NOT a blog, it is a serious science forum
(2) TEXTSPEAK is not allowed. Say PLEASE, not "please", and so forth.

As to the useability of ANY language, it depends somewhat on what you are going to use it for. Your quesion needs to be expanded.
 
  • #19
cmb said:
OK, so what does C++ return when you enter cos-¹(2)?

F77 returns a complex value.

After a few decades of Fortran programming, I was surprised to see that statement, so I checked, and http://gcc.gnu.org/onlinedocs/gcc-4.4.6/gfortran/ACOS.html says it's not correct (which is what I thought). It's not correct in Fortran 90 either.

If this is a non-standard extension to Fortran, it is a ridiculous way to create buggy code, unless the argument to ACOS is a complex expression.

I don't have any problem with ACOS((2, 0)) returning a complex value, but ACOS(2) should give an "out of range" error, not a complex value that is then silently truncated back to a real value.
 
  • #20
If you need a programming language for making calculations, you could use a program like Visual Basic, which will provide a lot of functions for you to use. If you're needing to make more complex calculations, you should use a language like C++.

For physics simulations, there are programming languages designed specifically for those.
 
  • #21
Hello everyone. Sorry to jump in and ask this question.
I want to make complex calculations and plot it's graphs, should I use Matlab? Can Matlab perform complex mathematical calculations, like calculating expressions that contain inverse trigonometric functions(OP's actual question), logarithms, matrices, etc.? I know Matlab can create graphs. How about C++. I have not seen C++ make any graphs. Is it possible to plot graphs using Java/C/C++? Tell me your best language which can do things like complex mathematical calculations and plotting graphs and physics simulations, or just complex mathematical calculations and plotting graphs, in a single environment. If there's any thing like that. Thanks.
 
Last edited:
  • #22
pairofstrings said:
Hello everyone. Sorry to jump in and ask this question.
I want to make complex calculations and plot it's graphs, should I use Matlab? Can Matlab perform complex mathematical calculations, like calculating expressions that contain inverse trigonometric functions(OP's actual question), logarithms, matrices, etc.? I know Matlab can create graphs. How about C++. I have not seen C++ make any graphs. Is it possible to plot graphs using Java/C/C++? Tell me your best language which can do things like complex mathematical calculations and plotting graphs and physics simulations, or just complex mathematical calculations and plotting graphs, in a single environment. If there's any thing like that. Thanks.

Most general purpose programming languages like C++ and Java won't have those graphical features integrated into them. You'll need to download libraries such as OpenGL to work with graphs and complex visuals. As for Matlab, and other scientific programming languages, it's designed specifically for working with things like graphs.
 
  • #23
Krunchyman said:
As for Matlab, and other scientific programming languages, it's designed specifically for working with things like graphs.

Okay. Can I even do complex mathematical calculations in Matlab along with plotting graphs of complex mathematical equations?
 
  • #24
pairofstrings said:
Okay. Can I even do complex mathematical calculations in Matlab along with plotting graphs of complex mathematical equations?

The basic structure in MATLAB is a matrix type of object.

The way you typically plot graphs in MATLAB is to create row vectors corresponding to your variables (like x and y), and MATLAB provides a lot of routines to generate vectors with certain values (like say for your x-axis), and defining functions is also very easy for calculating your f(x) for example.

If you want to use MATLAB, its a good idea to look up function handles, the linspace command, and the plot functions for what you have described above (functions and graphing).
 
  • #25
"Okay. Can I even do complex mathematical calculations in Matlab along with plotting graphs of complex mathematical equations?"

chiro said:
The basic structure in MATLAB is a matrix type of object.

The way you typically plot graphs in MATLAB is to create row vectors corresponding to your variables (like x and y), and MATLAB provides a lot of routines to generate vectors with certain values (like say for your x-axis), and defining functions is also very easy for calculating your f(x) for example.

If you want to use MATLAB, its a good idea to look up function handles, the linspace command, and the plot functions for what you have described above (functions and graphing).

Is that a yes? - you mean we can do complex mathematical calculations in Matlab? :smile:

Example:
Suppose I want to draw a graph for sec θ=x + 1/4x
I want 'θ' on the y-axis and different values of 'x' on x-axis. Can I plot the graph?

I am Sorry if I am going beyond the context of what should be asked in "Programming & Comp Sci" section. But if anyone here who is good at Matlab can explain me this. That would be great. Just want to know if it's possible to plot graph of complex mathematical functions after performing complex mathematical calculations(calculations involving Calculus, Algebra, Trigonometry, Probability), both done in the Matlab software. I will be doing Digital and Analog Signal Processing and Analysis. Purpose is to analyze signals/graph and generate random signals/graph. I don't want to switch softwares and languages after finding out that it's not useful for my purpose. I only know that Matlab can do things like recognition/detection.
 
Last edited:
  • #27
pairofstrings said:
"Okay. Can I even do complex mathematical calculations in Matlab along with plotting graphs of complex mathematical equations?"
Is that a yes? - you mean we can do complex mathematical calculations in Matlab? :smile:

Example:
Suppose I want to draw a graph for sec θ=x + 1/4x
I want 'θ' on the y-axis and different values of 'x' on x-axis. Can I plot the graph?

I am Sorry if I am going beyond the context of what should be asked in "Programming & Comp Sci" section. But if anyone here who is good at Matlab can explain me this. That would be great. Just want to know if it's possible to plot graph of complex mathematical functions after performing complex mathematical calculations(calculations involving Calculus, Algebra, Trigonometry, Probability), both done in the Matlab software. I will be doing Digital and Analog Signal Processing and Analysis. Purpose is to analyze signals/graph and generate random signals/graph. I don't want to switch softwares and languages after finding out that it's not useful for my purpose. I only know that Matlab can do things like recognition/detection.

You can draw these graphs very easily with Matlab. Or even Octave (which is a opensource software which is similar to Matlab.

For that matter, you can draw these graphs even in Microsoft Excel or Libre Office.
 

What factors should I consider when choosing a programming language?

When choosing a programming language, you should consider the purpose of your project, the level of experience of your team, the availability of resources and support, and the compatibility with other systems and tools.

Which programming language is the best for beginners?

Many experts recommend starting with a high-level language such as Python or Java for beginners. These languages have simpler syntax and are widely used, making it easier to find resources and support.

What is the difference between front-end and back-end languages?

Front-end languages, such as HTML, CSS, and JavaScript, are used to create the visual and interactive elements of a website or application. Back-end languages, such as Java, C++, and Python, are used to handle the server-side operations and data processing.

Which language is best for web development?

There is no one "best" language for web development, as it depends on the specific needs of the project. However, popular languages for web development include JavaScript, PHP, and Python.

Can I use multiple programming languages in one project?

Yes, it is common to use multiple programming languages in a project, especially in larger and more complex projects. For example, a web application may use HTML, CSS, JavaScript, and a back-end language like PHP or Python.

Similar threads

  • Programming and Computer Science
Replies
8
Views
832
Replies
65
Views
3K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
12
Replies
397
Views
13K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
990
Back
Top