numpy

58 views
Skip to first unread message

Girard Henri

unread,
Jan 9, 2018, 4:04:18 AM1/9/18
to sage-s...@googlegroups.com
Hi,

Happy new year all :)

I would like to know how to make this command numpy :

import numpy as np

times=np.arange ?

With sagemath command (not using arange), because I often don't need np
for example np.sqrt(), etc... As it is already in sage ?

I am using sagemath with few ext lib like pyknon, etc... Which doesn't
need numpy

Best

Henri

Simon King

unread,
Jan 9, 2018, 5:33:18 AM1/9/18
to sage-s...@googlegroups.com
Hi,

On 2018-01-09, Girard Henri <henri....@gmail.com> wrote:
> I would like to know how to make this command numpy :
>
> import numpy as np
>
> times=np.arange ?

What do you mean by "make this command"? numpy is included in SageMath.
Therefore,
import numpy as np
times = np.arange
just works.

Regards,
Simon

Girard Henri

unread,
Jan 9, 2018, 6:52:31 AM1/9/18
to sage-s...@googlegroups.com

Sorry ... I did'not notice



-------- Message transféré --------
Sujet : Re: [sage-support] Re: numpy
Date : Tue, 9 Jan 2018 12:46:13 +0100
De : Simon King <simon...@uni-jena.de>
Pour : Girard Henri <henri....@gmail.com>


Dear Girard,

please answer to the list, not to me privately. On the one hand, you are
more likely to get an answer when more than just one person reads your
question, and on the other hand, other users who might have the same
question as you will benefit from being able to read about it in
sage-support.

Best regards,
Simon


Am 09.01.2018 um 12:18 schrieb Girard Henri:
> An exemple
>
> from matplotlib import pyplot as plt
> from scipy.io import wavfile
> import numpy as np
> samplerate,data=wavfile.read("test.wav")
> times=np.arange(len(data))/float(samplerate)
> plt.plot(data[:1000])
>
>
>
> Le 09/01/2018 à 11:30, Simon King a écrit :
>> Hi,
>>
>> On 2018-01-09, Girard Henri <henri....@gmail.com> wrote:
>>> I would like to know how to make this command numpy :
>>>
>>> import numpy as np
>>>
>>> times=np.arange ?
>> What do you mean by "make this command"? numpy is included in SageMath.
>> Therefore,
>>    import numpy as np
>>    times = np.arange
>> just works.
>>
>> Regards,
>> Simon
>>
>

Simon King

unread,
Jan 9, 2018, 7:36:39 AM1/9/18
to sage-s...@googlegroups.com
Hi,

On 2018-01-09, Girard Henri <henri....@gmail.com> wrote:
> Am 09.01.2018 um 12:18 schrieb Girard Henri:
>> An exemple
>>
>> from matplotlib import pyplot as plt
>> from scipy.io import wavfile
>> import numpy as np
>> samplerate,data=wavfile.read("test.wav")
>> times=np.arange(len(data))/float(samplerate)
>> plt.plot(data[:1000])

What exactly goes wrong if you do the above in Sage? Please be more
specific.

In another message to me, you wrote:
> Most of the time I don't want to make heavier "import"... First because
> I don't master it and it's much better to do sqrt() instead np.sqrt() or
> np.pi()

I don't see why sqrt() is better than np.sqrt().

Anyway, as usual in Python, you can import functions like this:
sage: from numpy import sqrt
sage: sqrt
<ufunc 'sqrt'>
sage: sqrt(4)
2.0
sage: type(_)
<type 'numpy.float64'>

The disadvantage is that it overwrites Sage's sqrt() function. That's why
I believe it is better to do np.sqrt().

Best regards,
Simon

Girard Henri

unread,
Jan 9, 2018, 8:33:53 AM1/9/18
to sage-s...@googlegroups.com
Nothing goes wrong : My question is sage has already lot of functions
which don't need numpy, but if there is one function which needs numpy I
will have to add it. And arange needs np then I am asking if there a way
to do it in sage without using numpy, I am not an expert in sage and I
try to keep to pure command, I don't like to import lot of libs as they
are already declare in sage, I think is redondant... Then I just wanted
to know if there a way of doing np.arange but only with sage (not
importing numpy), for example matplolib uses lot of declaration like
plt... when i do a plot I only use plot(), I prefer look to sage plot
commands (even if they are from matplotlib) that's why I appreciate
sage, for latex it's very easy to have it straight because the libs are
in sage.  I use sage to do lot of work maths graphs draw music ... etc,
then I just add them with sage -pip and after I have nothing to remenber
for me sage is a kind of wisiwigsage all-in-one. I am a user not a
developer (pity for me... I would like to know this better), sage is the
best tool for math in my way. Easy, well documented and much more... I
do a lot of things (maybe not academic) but suiting to my needs... At
the moment I am in antic chinese music and this requires geometry /
music / math... sage do all :)

Regards

Henri

Emmanuel Charpentier

unread,
Jan 9, 2018, 1:53:22 PM1/9/18
to sage-support
Dear Henri,

When you do
import numpy as np
you are just creating a identifier that allows you to reach for a numpy function that happens to have the same name as another Sage|Python function. You do nor "import" physically anything.
So when you do :
foo=<whatever>
sqrt(foo)
you are in fact calling the Sage function sqrt, with the argument <whatever> ; that may or may not make sense to Sage.
But if you do
foo=np.<something>()
np.sqrt(foo)
you are calling the Numpy function sqrt, with an argument which is (probably) a Numpy object. np.sqrt(foo) may make sense (as opposed to, say, np.sqrt(bar), where bar is something without relation to Numpy...).


Le mardi 9 janvier 2018 14:33:53 UTC+1, HG a écrit :
Nothing goes wrong : My question is sage has already lot of functions
which don't need numpy, but if there is one function which needs numpy I
will have to add it.

Indeed. And you should note that this Numpy function may return something that is a Numpy object, that other Sage functions may or may not be able to use. You may have to convert it back to Sage.

A better example is given by Sympy. You may want to use, for example, its Laplace transform (and inverse thereof), which seem somewhat more capable than the native (Maxima's) one, or its Fourier transforms (which do not even exist in Maxima...).

import sympy
f=<some function of t>
var("s,t")
st,ss=sympy.symbols("st,ss")
F=sympy.fourier__transform(sympy.sympify(f),ss,st)
G=<somethingInterestingInvolvingF>
g=sympy.inverse_fourier_transform(G,st,ss)

At this point, g is something composed of sympy objects (IIRC, a tuup,e, whose first element is the sought inverse Fourier transform). In order to use it in Sage, you have to transform it back (using the ._sage_() method) and possibly rename some variables.

This mechanism has been automated for some operations : for example, that's (very roughly !) what the 'algorithm="sympy"' option of integrate() does.
 
And arange needs np then I am asking if there a way
to do it in sage without using numpy,

So in fact, you are looking for a "pure Sage" implementation of arange(). I do not know what does arange : it migh be trivial it might verge on the impossible, I do not know.

But the point is that mechanisms (which is relatively cheap) offers you a lot of power and flexibility : you can use the arange algorithm even if iot's not implemented|implementable in Sage...

Furthermore, you should consider if you need you result as a Sage object. For your purposes, in may be that (a part of) your algorithm is easier to express with Numpy ; so may be interested by implementing this part with Numpy and convert back to Sage only what you want in Sage...

--
Emmanuel Charpentier

Girard Henri

unread,
Jan 9, 2018, 2:17:00 PM1/9/18
to sage-s...@googlegroups.com

Dear Emmanuel,

Thank you very much for your explanations... What I should have said after reading you is "converting"

arange() does a sort of x,-2,2 for example (but I am not sure what it does that why I don't want to use something I don't understand. it's like np.linspace() I wonder if it doesn't come from matlab a kind of conversion ?

I think an academic person wouldn't bother about this and would use numpy or scipy, which I don't want to use as it is already in sage too, but transparent.

from __future__ import division,print_function, absolute_import
import struct,warnings,librosa 
import numpy as np
from IPython.display import Audio
def wavPlayer(data, rate):
    display(Audio(data, rate=rate))


from matplotlib import pyplot as plt
from scipy.io import wavfile

sr = 22050 # sample rate
T = 2.0    # seconds
t = np.linspace(0, T, int(T*sr), endpoint=False)
x = 0.5*np.sin(2*np.pi*440*t)
librosa.output.write_wav('la440.wav', x, sr)
samplerate,data=wavfile.read("la440.wav")
times=np.arange(len(data))/float(samplerate)
plt.plot(data[:1000]);
Audio(x, rate=sr)

This example shows my interrogation : I need a lot of import to do it... For this program I needed about 2 days looking in google how to find the functions. I don't complaint because people are nicely explaining me what I finished to notice... That's the wonder of mailing list of sage, one never feels lost or not long time.

Regards

Henri

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Emmanuel Charpentier

unread,
Jan 9, 2018, 5:31:05 PM1/9/18
to sage-support


Le mardi 9 janvier 2018 20:17:00 UTC+1, HG a écrit :

Dear Emmanuel,

Thank you very much for your explanations... What I should have said after reading you is "converting"

arange() does a sort of x,-2,2 for example (but I am not sure what it does that why I don't want to use something I don't understand.


Oh, puhleaaase ... What's so hard in
import numpy as np
np.arange?
Docstring:     
arange([start,] stop[, step,], dtype=None)

   Return evenly spaced values within a given interval.

   Values are generated within the half-open interval "[start, stop)"
   (in other words, the interval including start but excluding stop).
   For integer arguments the function is equivalent to the Python
   built-in range function, but returns an ndarray rather than a list.

   When using a non-integer step, such as 0.1, the results will often
   not be consistent.  It is better to use "linspace" for these cases.

( Elided for brevity...)
 

it's like np.linspace() I wonder if it doesn't come from matlab a kind of conversion ?

I think an academic person wouldn't bother about this and would use numpy or scipy, which I don't want to use as it is already in sage too, but transparent.


Again : since it's "in Sage", importing it adds nothing to your program (it's already in the van's boot). All you do by "import"ing is giving yourself a handle on it.
 

from __future__ import division,print_function, absolute_import
import struct,warnings,librosa 
import numpy as np
from IPython.display import Audio
def wavPlayer(data, rate):
    display(Audio(data, rate=rate))
from matplotlib import pyplot as plt
from scipy.io import wavfile
sr = 22050 # sample rate
T = 2.0    # seconds
t = np.linspace(0, T, int(T*sr), endpoint=False)
x = 0.5*np.sin(2*np.pi*440*t)
librosa.output.write_wav('la440.wav', x, sr)


Apparently, you use Numpy data as input to librosa.output.write. Check if this can accept a list of python floats. If so, you can get rid of Numpy and use Python drectly (but probably less efficiently).
 

samplerate,data=wavfile.read("la440.wav")


What is the type of the outputs of wavfile.read ?
 

times=np.arange(len(data))/float(samplerate)
plt.plot(data[:1000]);


Here, you use data (which type ?) as an input to plt.plot. Does the latter need Numpy objects ?
 

Audio(x, rate=sr)


Again what does Audio needs ?
 

This example shows my interrogation : I need a lot of import to do it... For this program I needed about 2 days looking in google how to find the functions. I don't complaint because people are nicely explaining me what I finished to notice... That's the wonder of mailing list of sage, one never feels lost or not long time.


The point of these imports is to reuse the work of other people. If they bothered to use Numpy and used this as their standard for data interchange (check the docs ! you can't escape that one...), you'd better use it too. Otherwise, you condemn yourself to reinvent the wheel (and probably end up with an excentered ellipsoid... Programming is easy, correct programming can be excruciatingly hard !).

More specifically : computing a sample of a sine wave is easy to do in Python. Plotting and playing it can be tricky. Use what you have to use to reuse the work of the libraries' authors.

HTH,

--
Emmanuel Charpentier

Girard Henri

unread,
Jan 9, 2018, 5:57:15 PM1/9/18
to sage-s...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages