Python Forum
dividing by list not possible?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dividing by list not possible?
#1
Greetings!

I was wondering why it is possible to create a new list by multiplying or adding constants to an old list, but not to create a new list through division of a float by an old list.

import numpy as np
import matplotlib.pyplot as plt

N=500;
R=0.5;

pressure_list=[16,25,40]
expectation_val_V=[445.186, 412.284, 390.5]
expectation_eta=(float(4)/3 *np.pi *R**3)/expectation_val_V

eta=np.linspace(0.5,0.70,100000)
p_red=6/(np.pi)*eta*( (12-3*(4*(1-(3*np.sqrt(2))/(np.pi)*eta)))/(4*(1-(3*np.sqrt(2))/(np.pi)*eta)) 
            +2.557696+0.1253077*(4*(1-(3*np.sqrt(2))/(np.pi)*eta)) 
            +0.1762393*(4*(1-(3*np.sqrt(2))/(np.pi)*eta))**2 
            -1.053308*(4*(1-(3*np.sqrt(2))/(np.pi)*eta))**3
            +2.818621*(4*(1-(3*np.sqrt(2))/(np.pi)*eta))**4 
            -2.921934*(4*(1-(3*np.sqrt(2))/(np.pi)*eta))**5
            +1.118413*(4*(1-(3*np.sqrt(2))/(np.pi)*eta))**6)


plt.plot(eta,p_red)
plt.show()
In this example, p_red is well defined. However, expectation_eta cannot be created. It gives the error 'TypeError: unsupported operand type(s) for /: 'float' and 'list''
Isn't this a bit contradictory from a programming point of view?

Regards!
Reply
#2
(Oct-01-2018, 09:58 PM)SchroedingersLion Wrote: Isn't this a bit contradictory from a programming point of view?
No, it depends on the implementation for a particular type/object. i.e. the creators of numpy implemented this functionality for numpy array object (that is what np.linspace returns). The python core developers did not implement that functionality for the list. You can use list comprehension instead. Or convert expectation_val_V to numpy array.
Also note that when you multiply a list by int constant, the new list is not constructed by multiplying individual elements of the original list.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Also, I think the numpy authors were trying to emulate matrix operations. Mathematicians don't divide matrices, mainly because matrix multiplication is not commutative, leading to an ambiguous definition of the inverse operation.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Oct-02-2018, 12:32 PM)ichabod801 Wrote: Also, I think the numpy authors were trying to emulate matrix operations. Mathematicians don't divide matrices, mainly because matrix multiplication is not commutative, leading to an ambiguous definition of the inverse operation.

Dividing and multiplying numpy.array is possible
Output:
In [19]: N=500 ...: R=0.5 ...: ...: pressure_list=np.array([16,25,40]) ...: expectation_val_V=np.array([445.186, 412.284, 390.5]) ...: expectation_eta=(4/3 *np.pi *R**3)/expectation_val_V ...: ...: In [20]: expectation_eta Out[20]: array([0.00117613, 0.00127 , 0.00134084])
Elements of numpy.array all have the same type
Output:
In [24]: pressure_list.dtype Out[24]: dtype('int64')
and if the dtype of an array allows for math operation (as long as shapes of arrays are compatible) - you are free to apply math operations on arrays.
Output:
In [51]: np.reshape(np.arange(6), (2, 3)) * np.arange(6, 9) Out[51]: array([[ 0, 7, 16], [18, 28, 40]])

list in Python is agnostic of the type of elements - which (element(s)) may be replaced by element(s) of another type dynamically, therefor math operations between lists (excluding addition - below) are meaningless.

List addition is basically interpreted as list concatenation
Output:
In [22]: list.__add__? Signature: list.__add__(self, value, /) Call signature: list.__add__(*args, **kwargs) Type: wrapper_descriptor String form: <slot wrapper '__add__' of 'list' objects> Namespace: Python builtin Docstring: Return self+value.

PS

numeric numpy.array may also be multiplied or divided by a scalar digital value; power op is applicable too

Output:
In [52]: np.arange(6, 9) ** 2 Out[52]: array([36, 49, 64])

PPS
WHat is inefficient - from any point of view - is the calculation of the expression below several times; redundant brackets - together with lack of spaces - reduce readability too
(4*(1-(3*np.sqrt(2))/(np.pi)*eta))
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
Thank you guys! Almost forgot about the thread, sorry!

@volcano63
So if I want to use these elementwise array operations, I have to make sure that I am using an actual "numpy array" and not the standard Python lists.

volcano63 Wrote:PPS
WHat is inefficient - from any point of view - is the calculation of the expression below several times; redundant brackets - together with lack of spaces - reduce readability too
Yeah sorry, I added the brackets because I did not know how well Python deals with the order of mathematical operation:
Can I write it like this?
4*(1 - 3*np.sqrt(2)/np.pi *eta)
And yeah, I should have used a variable instead, thanks for the hint!
Reply
#6
(Oct-11-2018, 09:00 AM)SchroedingersLion Wrote: Thank you guys! Almost forgot about the thread, sorry!

@volcano63
So if I want to use these elementwise array operations, I have to make sure that I am using an actual "numpy array" and not the standard Python lists.

Right, one of the operators must be numpy array; another may be either an array of compatible shape or a scalar numeric value
Output:
In [11]: a = np.arange(6, 9) In [12]: print(9 / a, a / 9, a ** 2, 2 ** a, sep='\n') [1.5 1.28571429 1.125 ] [0.66666667 0.77777778 0.88888889] [36 49 64] [ 64 128 256]

(Oct-11-2018, 09:00 AM)SchroedingersLion Wrote:
volcano63 Wrote:PPS
WHat is inefficient - from any point of view - is the calculation of the expression below several times; redundant brackets - together with lack of spaces - reduce readability too
Yeah sorry, I added the brackets because I did not know how well Python deals with the order of mathematical operation:
Can I write it like this?
4*(1 - 3*np.sqrt(2)/np.pi *eta)
And yeah, I should have used a variable instead, thanks for the hint!

PEP-8 expression guidlines recommend - among other things - to surround operators by single spaces, so the following form is fully compatible, and sparse enough to be readable
4 * (1 - 3 * np.sqrt(2) / np.pi * eta)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,412 Mar-14-2020, 09:19 AM
Last Post: scidam
  When dividing elements of matrix i would like the result 0 instead of inf? Koczkodan 4 2,856 Jul-22-2019, 11:40 AM
Last Post: Koczkodan
  linspace not dividing equal intervals sheel 0 2,365 Jan-16-2018, 04:28 PM
Last Post: sheel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020