Python Forum
dividing by list not possible?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dividing by list not possible?
#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


Messages In This Thread
RE: dividing by list not possible? - by buran - Oct-02-2018, 07:03 AM
RE: dividing by list not possible? - by ichabod801 - Oct-02-2018, 12:32 PM
RE: dividing by list not possible? - by volcano63 - Oct-02-2018, 04:43 PM
RE: dividing by list not possible? - by volcano63 - Oct-11-2018, 10:00 AM

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,486 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,974 Jul-22-2019, 11:40 AM
Last Post: Koczkodan
  linspace not dividing equal intervals sheel 0 2,413 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