Re: What is the simplest method to get a vector result?

2014-07-25 Thread fl
On Friday, July 25, 2014 8:37:14 PM UTC-4, Ian wrote:
> On Fri, Jul 25, 2014 at 5:08 PM, fl  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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-25 Thread Ian Kelly
On Fri, Jul 25, 2014 at 5:08 PM, fl  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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-25 Thread fl
On Thursday, July 24, 2014 9:49:14 AM UTC-4, Vlastimil Brom wrote:
> 2014-07-24 14:53 GMT+02:00 fl :
> 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,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-24 Thread Steven D'Aprano
On Thu, 24 Jul 2014 22:44:51 -0400, Dave Angel wrote:

> fl  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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-24 Thread Dave Angel
fl  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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-24 Thread Ian Kelly
On Thu, Jul 24, 2014 at 7:29 PM, fl  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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-24 Thread fl
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 ("   * " " + "*"   ").









>>>   *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
  *
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-24 Thread Marko Rauhamaa
fl :

> 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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-24 Thread Steven D'Aprano
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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-24 Thread Vlastimil Brom
2014-07-24 14:53 GMT+02:00 fl :
> 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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the simplest method to get a vector result?

2014-07-24 Thread Alan
You can use `list(math.sin(x * math.pi / 2) for x in index)` or use `numpy`, 
which supports array math.
-- 
https://mail.python.org/mailman/listinfo/python-list