dividing by list not possible?
 dividing by list not possible? SchroedingersLion Silly Frenchman Posts: 40 Threads: 13 Joined: Jul 2018 Reputation: Oct-01-2018, 09:58 PM 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 buran Posts: 7,700 Threads: 137 Joined: Sep 2016 Reputation: Oct-02-2018, 07:03 AM (This post was last modified: Oct-02-2018, 07:03 AM by buran.) (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 ichabod801 Bunny Rabbit Posts: 4,231 Threads: 97 Joined: Sep 2016 Reputation: Oct-02-2018, 12:32 PM 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 volcano63 Verb Conjugator Posts: 566 Threads: 10 Joined: Apr 2017 Reputation: Oct-02-2018, 04:43 PM (This post was last modified: Oct-02-2018, 04:43 PM by volcano63.) (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 : 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 : expectation_eta Out: array([0.00117613, 0.00127 , 0.00134084])Elements of numpy.array all have the same type Output:In : pressure_list.dtype Out: 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 : np.reshape(np.arange(6), (2, 3)) * np.arange(6, 9) Out: 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 : list.__add__? Signature: list.__add__(self, value, /) Call signature: list.__add__(*args, **kwargs) Type: wrapper_descriptor String form: 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 : np.arange(6, 9) ** 2 Out: 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 SchroedingersLion Silly Frenchman Posts: 40 Threads: 13 Joined: Jul 2018 Reputation: Oct-11-2018, 09:00 AM 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 tooYeah 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 volcano63 Verb Conjugator Posts: 566 Threads: 10 Joined: Apr 2017 Reputation: Oct-11-2018, 10:00 AM (This post was last modified: Oct-11-2018, 10:36 AM by volcano63.) (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 : a = np.arange(6, 9) In : 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 tooYeah 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 Dividing a single column of dataframe into multiple columns based on char length darpInd 2 1,119 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 1,644 Jul-22-2019, 11:40 AM Last Post: Koczkodan linspace not dividing equal intervals sheel 0 1,658 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