Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75140 > unrolled thread

What is the simplest method to get a vector result?

Started byfl <rxjwg98@gmail.com>
First post2014-07-24 05:53 -0700
Last post2014-07-25 03:23 +0000
Articles 12 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  What is the simplest method to get a vector result? fl <rxjwg98@gmail.com> - 2014-07-24 05:53 -0700
    Re: What is the simplest method to get a vector result? Alan <alan.isaac@gmail.com> - 2014-07-24 06:10 -0700
    Re: What is the simplest method to get a vector result? Vlastimil Brom <vlastimil.brom@gmail.com> - 2014-07-24 15:49 +0200
      Re: What is the simplest method to get a vector result? fl <rxjwg98@gmail.com> - 2014-07-25 16:08 -0700
        Re: What is the simplest method to get a vector result? Ian Kelly <ian.g.kelly@gmail.com> - 2014-07-25 18:37 -0600
          Re: What is the simplest method to get a vector result? fl <rxjwg98@gmail.com> - 2014-07-25 19:17 -0700
    Re: What is the simplest method to get a vector result? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-24 14:06 +0000
    Re: What is the simplest method to get a vector result? Marko Rauhamaa <marko@pacujo.net> - 2014-07-24 17:25 +0300
      Re: What is the simplest method to get a vector result? fl <rxjwg98@gmail.com> - 2014-07-24 18:29 -0700
        Re: What is the simplest method to get a vector result? Ian Kelly <ian.g.kelly@gmail.com> - 2014-07-24 19:50 -0600
        Re: What is the simplest method to get a vector result? Dave Angel <davea@davea.name> - 2014-07-24 22:44 -0400
          Re: What is the simplest method to get a vector result? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-25 03:23 +0000

#75140 — What is the simplest method to get a vector result?

Fromfl <rxjwg98@gmail.com>
Date2014-07-24 05:53 -0700
SubjectWhat is the simplest method to get a vector result?
Message-ID<95524a09-7792-4cec-8c7e-7525a358ee8c@googlegroups.com>
Hi,
I have read a lot about Python, but it still has a problem now on a simple
exercise. For example, I want to generate a sine curve. First, I get a time 
sequence: 

index=range(100)

I import math module, try to calculate sine with

math.sin(index*math.pi/2)

but it fails.

It is possible to use a for loop, but I don't know how to save each result to an
array.

I have gone through several tutorial, but they are all about using print right
away. I want to get an array, or a series, then plot it with 

import matplotlib.pyplot as plt

I have installed plot module and it works already. I am a little hurry for an 
project interview and would like to ask here besides I continue search on 
tutorial.


Thanks,

[toc] | [next] | [standalone]


#75141

FromAlan <alan.isaac@gmail.com>
Date2014-07-24 06:10 -0700
Message-ID<56e9ec65-916e-45f8-a937-3ad4eeb18a79@googlegroups.com>
In reply to#75140
You can use `list(math.sin(x * math.pi / 2) for x in index)` or use `numpy`, which supports array math.

[toc] | [prev] | [next] | [standalone]


#75142

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2014-07-24 15:49 +0200
Message-ID<mailman.12275.1406209762.18130.python-list@python.org>
In reply to#75140
2014-07-24 14:53 GMT+02:00 fl <rxjwg98@gmail.com>:
> Hi,
> I have read a lot about Python, but it still has a problem now on a simple
> exercise. For example, I want to generate a sine curve. First, I get a time
> sequence:
>
> index=range(100)
>
> I import math module, try to calculate sine with
>
> math.sin(index*math.pi/2)
>
> but it fails.
>
> It is possible to use a for loop, but I don't know how to save each result to an
> array.
>
> I have gone through several tutorial, but they are all about using print right
> away. I want to get an array, or a series, then plot it with
>
> import matplotlib.pyplot as plt
>
> I have installed plot module and it works already. I am a little hurry for an
> project interview and would like to ask here besides I continue search on
> tutorial.
>
>
> Thanks,
> --

Hi,
depending on your actual needs, you may also try a specialised library
like mpmath, which also supports plotting (and uses matplotlib
internally):
http://mpmath.org/
Using the sensible defaults, the plotting of a function can be as simple as:

mpmath.plot(mpmath.sin)

As for your original question, you can use a library designed for
working with this data:
http://www.numpy.org/

numpy.arange(100) * numpy.pi

hth,
   vbr

[toc] | [prev] | [next] | [standalone]


#75214

Fromfl <rxjwg98@gmail.com>
Date2014-07-25 16:08 -0700
Message-ID<037589dd-7f05-4a8d-a7a5-dcbb7cf2db12@googlegroups.com>
In reply to#75142
On Thursday, July 24, 2014 9:49:14 AM UTC-4, Vlastimil Brom wrote:
> 2014-07-24 14:53 GMT+02:00 fl <rxj..98@gmail.com>:
> internally):
> http://mpmath.org/
> Using the sensible defaults, the plotting of a function can be as simple as:
> 
> mpmath.plot(mpmath.sin)
> 
> As for your original question, you can use a library designed for
> working with this data:
> http://www.numpy.org/
> 
> numpy.arange(100) * numpy.pi
> 
> 
> hth,
> 
>    vbr

I want to use your reply about numpy, but I find only the following works:

import numpy as numpy

Do you have other ways to import? (I find the above import a little more letters)

Thanks,

[toc] | [prev] | [next] | [standalone]


#75218

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-07-25 18:37 -0600
Message-ID<mailman.12323.1406335083.18130.python-list@python.org>
In reply to#75214
On Fri, Jul 25, 2014 at 5:08 PM, fl <rxjwg98@gmail.com> wrote:
> I want to use your reply about numpy, but I find only the following works:
>
> import numpy as numpy
>
> Do you have other ways to import? (I find the above import a little more letters)

What's wrong with:

import numpy

[toc] | [prev] | [next] | [standalone]


#75222

Fromfl <rxjwg98@gmail.com>
Date2014-07-25 19:17 -0700
Message-ID<830fcfcf-ef69-480a-9051-592e50a7438c@googlegroups.com>
In reply to#75218
On Friday, July 25, 2014 8:37:14 PM UTC-4, Ian wrote:
> On Fri, Jul 25, 2014 at 5:08 PM, fl <rxj--@gmail.com> wrote:
> > Do you have other ways to import? (I find the above import a little more letters)
> 
> What's wrong with:
> 
> import numpy

I was wrong, maybe some careless key inputs. "import numpy" works.

[toc] | [prev] | [next] | [standalone]


#75143

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-07-24 14:06 +0000
Message-ID<53d112e0$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#75140
On Thu, 24 Jul 2014 05:53:12 -0700, fl wrote:

> Hi,
> I have read a lot about Python, but it still has a problem now on a
> simple exercise. For example, I want to generate a sine curve. First, I
> get a time sequence:
> 
> index=range(100)
> 
> I import math module, try to calculate sine with
> 
> math.sin(index*math.pi/2)
> 
> but it fails.
> 
> It is possible to use a for loop, but I don't know how to save each
> result to an array.

That is a fundamental, basic part of programming in Python. If you don't 
learn the basics, you will never be a good programmer. Have you done any 
Python tutorials? It's not enough to read them, you must actually do the 
work.

This might get you started:


results = []  # Start with an empty list.
for value in range(100):
    results.append(math.sin(value))


but that probably will not give you the results you are hoping for, as 
sin expects the angles to be given in radians.

Perhaps you mean to scale the angles over a full circle?


tau = 2*math.pi  # number of radians in a full circle
angles = []
values = []
for i in range(100):
    angle = i*tau/100.0
    angles.append(angle)
    values.append(math.sin(angle))


Alternatively, here's a slightly shorter way:

angles = [i*tau/100.0 for i in range(100)]
values = [math.sin(angle) for angle in angles]



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#75144

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-07-24 17:25 +0300
Message-ID<8761imu93j.fsf@elektro.pacujo.net>
In reply to#75140
fl <rxjwg98@gmail.com>:

> I have read a lot about Python, but it still has a problem now on a
> simple exercise. For example, I want to generate a sine curve.

Here you go:

========================================================================
#!/usr/bin/env python3

import math

for x in range(0, 361, 15):
    print(int((math.sin(x / 180 * math.pi) + 1) * 30 + 0.5) * " " + "*")
========================================================================


Marko

[toc] | [prev] | [next] | [standalone]


#75191

Fromfl <rxjwg98@gmail.com>
Date2014-07-24 18:29 -0700
Message-ID<ffce5f1b-91b6-4ac1-99dc-e3eef51694d3@googlegroups.com>
In reply to#75144
On Thursday, July 24, 2014 10:25:52 AM UTC-4, Marko Rauhamaa wrote:
> #!/usr/bin/env python3
> 
> import math
> 
> for x in range(0, 361, 15):
> 
>     print(int((math.sin(x / 180 * math.pi) + 1) * 30 + 0.5) * " " + "*")
> 
> ========================================================================
> 
> 
> Marko

I like your method, but I get a column of '*'. Maybe you have other intentions
of your code. I am puzzled about the last part of your code and want to learn
from it ("   * " " + "*"   ").









>>>                               *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *
                              *

[toc] | [prev] | [next] | [standalone]


#75192

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-07-24 19:50 -0600
Message-ID<mailman.12307.1406253077.18130.python-list@python.org>
In reply to#75191
On Thu, Jul 24, 2014 at 7:29 PM, fl <rxjwg98@gmail.com> wrote:
> On Thursday, July 24, 2014 10:25:52 AM UTC-4, Marko Rauhamaa wrote:
>> #!/usr/bin/env python3
>>
>> import math
>>
>> for x in range(0, 361, 15):
>>
>>     print(int((math.sin(x / 180 * math.pi) + 1) * 30 + 0.5) * " " + "*")
>>
>> ========================================================================
>>
>>
>> Marko
>
> I like your method, but I get a column of '*'. Maybe you have other intentions
> of your code. I am puzzled about the last part of your code and want to learn
> from it ("   * " " + "*"   ").

You probably ran it with Python 2. That code was written for Python 3
and assumes that division of two ints will return a float.  You can
also fix it by adding the line "from __future__ import division" at
the top of the file.

[toc] | [prev] | [next] | [standalone]


#75193

FromDave Angel <davea@davea.name>
Date2014-07-24 22:44 -0400
Message-ID<mailman.12308.1406256291.18130.python-list@python.org>
In reply to#75191
fl <rxjwg98@gmail.com> Wrote in message:

> 
>  I am puzzled about the last part of your code and want to learn
> from it ("   * " " + "*"   ").
> 

6 * " " + "x"

will produce a string of 6 blanks followed by an x.



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#75194

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-07-25 03:23 +0000
Message-ID<53d1cdb1$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#75193
On Thu, 24 Jul 2014 22:44:51 -0400, Dave Angel wrote:

> fl <rxjwg98@gmail.com> Wrote in message:
> 
> 
>>  I am puzzled about the last part of your code and want to learn
>> from it ("   * " " + "*"   ").
>> 
>> 
> 6 * " " + "x"
> 
> will produce a string of 6 blanks followed by an x.


Dave is correct, but if you want to format text to a certain width, it is 
better to have Python count how many spaces you need:


py> 'spam'.rjust(20)
'                spam'
py> 'spam'.ljust(20)
'spam                '
py> 'spam'.center(20)
'        spam        '


In recent versions, you can even specify the fill character:

py> 'spam'.center(20, '.')
'........spam........'


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web