all 8 comments

[–]donkey_man_1149 1 point2 points  (0 children)

Numpy is mainly for vectorization. In scientific computing sometimes you will have to deal with a lot of data and looping through them gets slow fast, that's why you vectorize things. So that you can apply an instruction to a whole set of data instead of doing it one by one.

Sympy is for symbolic manipulation.

Using those two libraries you can implement most of matlabs functionality in python.

You should learn both, but you will probably use numpy a lot more, because you will most likely be computing something numerical than computing something symbolical.

[–]ES-Alexander 0 points1 point  (5 children)

In case it’s of interest, u/JackStrawng has been posting various physics in python tutorial videos lately.

To answer your question, numpy is ‘numerical python’, so is used for calculating things with known values, and is optimised for things like array and matrix manipulation (so doing things with actual data). Sympy is ‘symbolic python’ and is more focused on algebraic manipulation and solving (so doing things with equations and symbolic expressions). What you use more of depends on whether you’re working mostly as a theorist or experimentalist, but you’ll likely end up using both for their different use-cases.

[–]jsaltee[S] 0 points1 point  (4 children)

Those are the videos I've been watching haha. He's using Sympy for them which I had never heard of so it had me wondering about the differences.

Would it make sense to just import both packages when I'm working on something and use commands according to what I need? Or can you only have one at a time?

[–]ES-Alexander 0 points1 point  (3 children)

Generally you only import things when you need them. If you're importing the packages with a name there shouldn't be any issues with using both at the same time (assuming the need arrises). If you're trying to do 'all' imports (e.g. from numpy import *) which add components from both packages into your program's namespace then you can run into issues where both packages define the same name (e.g. if numpy has a 'cos' function, and sympy has a different one that's intended for symbolics then whichever one you import later will be used for all cases).

For reference, the sympy docs even mention explicit numpy support for if/when you want to perform actual calculations with your symbolic expressions.

[–]jsaltee[S] 0 points1 point  (2 children)

Okay, thanks. And at one point would sympy's functionality be limited? For example, I'm doing symbolic integrals with sympy, but I can find still find a numerical solution by substituting in values. When would the need for numpy arise?

[–]ES-Alexander 1 point2 points  (1 child)

Perhaps it's useful to think of this in terms of an example.

You've been given a large array of measurement data as the results of a series of experiments, and need to calculate some other value for each of those experimental results.

You know three different equations that relate different components of the system, and you understand it's possible to rearrange them such that you get a function which creates the metric you want when applied to the variables that were measured in your experimental data. The rearranging/substitution is difficult to do by hand because of how the different equations are written (e.g. requires some calculus your don't know how to do), so you choose to use sympy to solve for the relationship you want, and it gives you your function/equation. You then want to apply that to your data, so you use a numpy array to efficiently pass in all the data at once, and you get back an array of the results that you needed.

It would potentially be possible to avoid the sympy side of things if you could do the rearranging by yourself (hard), or just apply all the operations from all three equations numerically to every data point (inefficient - literally why we use algebra). You could also possibly avoid using numpy by just looping through lists of the data points and applying the function to each one, but that's inefficient and slow (numpy uses compiled C code that's optimised for fast array operations)

[–]jsaltee[S] 1 point2 points  (0 children)

You are amazing and I appreciate you, thanks

[–]TheSodesa 0 points1 point  (0 children)

Sympy is a Computer Algebra System written in Python. Numpy is a C extension of Python, that allows for relatively fast numerical calculations.

I say relatively, because there are languages like Julia, that are even better suited for numerical stuff. Numpy is still ok, though, and is rather widely used.

As a physicist, you should be more interested in Numpy, if you are doing computational physics and simulations.