Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended qnArrayTwo class to include all ufuncs which make sense #2

Open
wants to merge 106 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
93be5e4
added new html_str property to quantity to provide html formatting
mayya-bondarevskaya Feb 28, 2017
d28840a
resolve encoding issues
mayya-bondarevskaya Jun 27, 2017
016fb33
add basic initialisation of qnArray class
mayya-bondarevskaya Jun 27, 2017
8f097a2
minor qnArray compatibility resolvement
mayya-bondarevskaya Jun 27, 2017
b7f3a36
allow division with qnArray as well
mayya-bondarevskaya Jun 27, 2017
1af4204
add division function to qnArray class
mayya-bondarevskaya Jun 27, 2017
bf424f9
begin testing of qnArray class
mayya-bondarevskaya Jun 27, 2017
9759a6d
add new error-raising features
mayya-bondarevskaya Jun 27, 2017
fa68171
add more qnArray initialisation tests
mayya-bondarevskaya Jun 27, 2017
84d8ceb
add check for arrays of strings as input values
mayya-bondarevskaya Jun 28, 2017
d4a999f
add compatibility with qn_array
mayya-bondarevskaya Jun 28, 2017
02298fc
add tests
mayya-bondarevskaya Jun 28, 2017
1460451
change numpy array from containing Quantity objects to containing an …
mayya-bondarevskaya Jul 3, 2017
d355195
add multiplication test
mayya-bondarevskaya Jul 3, 2017
d2f94c9
add more qnArray multiplication tests
mayya-bondarevskaya Jul 3, 2017
bb32317
add test for other object type to be only Quantity
mayya-bondarevskaya Jul 3, 2017
22f6224
add qnArray division test
mayya-bondarevskaya Jul 3, 2017
14168e4
add more comprehensive error testing
mayya-bondarevskaya Jul 3, 2017
ef49589
separate exception raising situations into separate tests
mayya-bondarevskaya Jul 3, 2017
3f4fd2a
another variation of qn_array
mayya-bondarevskaya Jul 4, 2017
236411d
add tests to qnArrayTwo class
mayya-bondarevskaya Jul 4, 2017
98d39d2
add testing of standard deviation initialisation in qnArrayTwo
mayya-bondarevskaya Jul 4, 2017
2a9b766
add function created
mayya-bondarevskaya Jul 4, 2017
33868f3
add function in qnArrayTwo tested
mayya-bondarevskaya Jul 4, 2017
30f8afc
add functin to subtract qnArrayTwo objects
mayya-bondarevskaya Jul 4, 2017
aecb9da
add tests for subtracting qnArrayTwo objects
mayya-bondarevskaya Jul 4, 2017
1d5a0ec
add multiplication function
mayya-bondarevskaya Jul 4, 2017
1b8e93a
add testing of multiplication of qnArrayTwo objects
mayya-bondarevskaya Jul 4, 2017
f8e27c9
add division function
mayya-bondarevskaya Jul 4, 2017
c0e80cc
test division function
mayya-bondarevskaya Jul 4, 2017
dfb73da
add standard deviation calculations to algebra function
mayya-bondarevskaya Jul 4, 2017
0b6e892
test standard deivation calculations during algebraic calculations
mayya-bondarevskaya Jul 4, 2017
dfbb8ef
add standard deviation calculation to __pow__ function
mayya-bondarevskaya Jul 4, 2017
306b4de
add __pow__ function to qnArrayTwo class
mayya-bondarevskaya Jul 4, 2017
0a53c63
test qnArrayTwo __pow__ function
mayya-bondarevskaya Jul 4, 2017
b6f9c09
added __eq__ function but facing recursion depth exceded error
mayya-bondarevskaya Jul 4, 2017
1c8033f
prepare tests for not currently functioning __eq__ function
mayya-bondarevskaya Jul 4, 2017
fd6db2a
add html_str function to output html style representation of the array
mayya-bondarevskaya Jul 4, 2017
df8068b
add tests for html_str function
mayya-bondarevskaya Jul 4, 2017
73f4e27
add test file for Dimensions class
mayya-bondarevskaya Jul 6, 2017
f28400c
add DimensionsError class to handle exceptions
mayya-bondarevskaya Jul 6, 2017
3986d72
add tests for usage of DimensionsError class and when it arises furin…
mayya-bondarevskaya Jul 6, 2017
f562e10
add tests for Dimensions objects mutiplication and division
mayya-bondarevskaya Jul 6, 2017
73e5629
add tests for Dimensions class for __pow__ and __eq__
mayya-bondarevskaya Jul 6, 2017
95fb38b
add dimensions __neq__ function tests
mayya-bondarevskaya Jul 7, 2017
c3e923a
add testing of kwargs during init
mayya-bondarevskaya Jul 7, 2017
fc24734
add test for var type in dims list upon init
mayya-bondarevskaya Jul 7, 2017
e8ae7d7
add tests for Dimensions initialisation using key word args
mayya-bondarevskaya Jul 7, 2017
fe4a31d
add tests for Dimensions class str representation
mayya-bondarevskaya Jul 7, 2017
4a0b43e
add test for symbol class init
mayya-bondarevskaya Jul 7, 2017
dc505c0
add str output to symbol in case no name is supplied
mayya-bondarevskaya Jul 7, 2017
8eb3768
add test for str representation of symbol class
mayya-bondarevskaya Jul 7, 2017
cc6ef62
add tests of si_prefixes
mayya-bondarevskaya Jul 7, 2017
85cf58e
add test for SIPrefix init
mayya-bondarevskaya Jul 7, 2017
398c83b
add test for BaseUnit class
mayya-bondarevskaya Jul 10, 2017
87d72d3
add test for equality between base units
mayya-bondarevskaya Jul 10, 2017
e9cdcbb
add test for str representation of base units
mayya-bondarevskaya Jul 10, 2017
e140da8
add SIPrefix pbject comparison function
mayya-bondarevskaya Jul 10, 2017
3a79b18
add SIPrefix object comparison tests
mayya-bondarevskaya Jul 10, 2017
06866cd
add Quantity.parse function tests
mayya-bondarevskaya Jul 10, 2017
b43d274
remove sd parameter in favour of speed, adjust all algebraic operatio…
mayya-bondarevskaya Jul 12, 2017
a7a6a9b
adjust tests to not include sd parameter in the qnArrayTwo class init…
mayya-bondarevskaya Jul 12, 2017
79b9ace
add radd, rsub, rmul, and rtruediv functions to qnArrayTwo class
mayya-bondarevskaya Jul 12, 2017
ead8019
add possiblity to add, divide, multiply, and subtract by qnArrayTwo o…
mayya-bondarevskaya Jul 12, 2017
d3eafcf
add tests for all combinations of algebraic operations
mayya-bondarevskaya Jul 12, 2017
ca2a490
add __eq__ function to qnArrayTwo
mayya-bondarevskaya Jul 12, 2017
a4d414f
add tests for __eq__ function
mayya-bondarevskaya Jul 12, 2017
cc6906c
fix html_str functions results to not include standard deviation and …
mayya-bondarevskaya Jul 12, 2017
de2bb0c
add initialisation of Quantity object standard deviation to 0 if None…
mayya-bondarevskaya Jul 13, 2017
ca3eeca
add standard deviation initialisation and calculation when adding/sub…
mayya-bondarevskaya Jul 13, 2017
daac867
add tests for manipulation of standard deviation when adding qn arrays
mayya-bondarevskaya Jul 13, 2017
d5daae4
replace algebraic functions with predefined ufuncs using __array_ufunc__
mayya-bondarevskaya Jul 13, 2017
16ae6a1
add test for AtomUnit init
mayya-bondarevskaya Jul 13, 2017
7789815
add test for pow of AtomUnit
mayya-bondarevskaya Jul 13, 2017
c4a2046
fix quantity class to provide result to __pow__ function using Quanti…
mayya-bondarevskaya Jul 10, 2017
2039ece
implement radd, rsub, rmul, and rtruediv functions
mayya-bondarevskaya Jul 19, 2017
61a2428
add future tests for other ufuncs
mayya-bondarevskaya Jul 19, 2017
a0ef262
implement exp ufunc
mayya-bondarevskaya Jul 19, 2017
90fee7d
fix testing equation
mayya-bondarevskaya Jul 19, 2017
5995aad
implement sin and cos ufuncs
mayya-bondarevskaya Jul 19, 2017
d4ddb9e
add tests for sin and cos ufuncs
mayya-bondarevskaya Jul 19, 2017
e856c31
implement more arcsin, arccos, arctan, sinh, cosh, tanh, arcsinh, arc…
mayya-bondarevskaya Jul 19, 2017
12a5dc9
implement tests for arcsin, arccos, and arctan ufuncs
mayya-bondarevskaya Jul 19, 2017
c03ed2f
add more tests for sd prop
mayya-bondarevskaya Jul 20, 2017
064eefb
add conversion function
mayya-bondarevskaya Jul 20, 2017
04783e6
add tests for qnArrayTwo conversion
mayya-bondarevskaya Jul 20, 2017
a7bd632
add function to append values to qnArrayTwo and to plot two qnArrayTw…
mayya-bondarevskaya Jul 20, 2017
b25fe35
test append function
mayya-bondarevskaya Jul 20, 2017
3fa23b4
add unit checking for ufuncs, make append function only accept Quanti…
mayya-bondarevskaya Jul 24, 2017
cab9536
adjust tests for appending only Quantities to qnArrayTwo types
mayya-bondarevskaya Jul 24, 2017
33ed106
add more robust testing mechanism for ufuncs
mayya-bondarevskaya Jul 25, 2017
a602c03
add true_divide, floor_divide, and negative ufuncs
mayya-bondarevskaya Jul 25, 2017
afb2f0c
complete testing of all ufuncs in ufunc_dict_one_input
mayya-bondarevskaya Jul 26, 2017
05e5c78
add changing of units given a ufunc with two inputs
mayya-bondarevskaya Jul 26, 2017
93d79e1
add testing of ufuncs with two inputs
mayya-bondarevskaya Jul 26, 2017
00f9cf6
add checking of all ufuncs with two inputs
mayya-bondarevskaya Jul 26, 2017
869dffc
add all math operator ufuncs that make sense in the qnArrayTwo context
mayya-bondarevskaya Jul 26, 2017
2bca371
fix minor bug
mayya-bondarevskaya Jul 27, 2017
37e3abe
rearrange testing mechanism to loop over reworked ufuncs
mayya-bondarevskaya Jul 27, 2017
2ac3fdb
add separate file for ufuncs and error calculation function
mayya-bondarevskaya Jul 27, 2017
215c295
fix minor bugs
mayya-bondarevskaya Jul 26, 2017
d440ae6
remove original qnArray and replace with qnArrayTwo as primary array …
mayya-bondarevskaya Aug 1, 2017
17a2cdd
add more meaningful commenting of class methods
mayya-bondarevskaya Aug 1, 2017
44a8bf2
add preamble to file
mayya-bondarevskaya Aug 1, 2017
c8ba250
add units checking for addition and subtraction of QnArray objects
mayya-bondarevskaya Aug 7, 2017
8dc6146
fix conversion to mol
mayya-bondarevskaya Aug 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
replace algebraic functions with predefined ufuncs using __array_ufunc__
mayya-bondarevskaya committed Jul 13, 2017
commit d5daae4feeea78426a9e076af65e72d5137210fb
101 changes: 36 additions & 65 deletions pyqn/qn_array_two.py
Original file line number Diff line number Diff line change
@@ -29,72 +29,38 @@ def __new__(cls, input_array, info=None, units='1', sd=None):
def __array_finalize__(self, obj):
if obj is None: return
self.info = getattr(obj, 'info', None)

def __add__(self, other):
""" Function for adding a Quantity value to all values in
qnArrayTwo or adding another anArrayTwo to the current array
"""
if type(other) is qnArrayTwo:
if self.units != other.units:
raise qnArrayTwoError('Units of the two array must be compatible')
return qnArrayTwo(super(qnArrayTwo, self).__add__(super(qnArrayTwo, other)), units = self.units, sd = np.sqrt(self.sd**2+other.sd**2))
if type(other) is Quantity:
if self.units != other.units:
raise qnArrayTwoError('Units of the two array must be compatible')
return qnArrayTwo(super(qnArrayTwo, self).__add__(other.value), units = self.units, sd = np.sqrt(self.sd**2+other.sd**2))
return qnArrayTwo(super(qnArrayTwo, self).__add__(other), units = self.units, sd = self.sd)

def __radd__(self, other):
return self.__add__(other)
self.sd = getattr(obj, 'sd', None)

def __sub__(self, other):
if type(other) is qnArrayTwo:
if self.units != other.units:
raise qnArrayTwoError('Units of the two array must be compatible')
return qnArrayTwo(super(qnArrayTwo, self).__sub__(super(qnArrayTwo, other)), units = self.units, sd = np.sqrt(self.sd**2+other.sd**2))
if type(other) is Quantity:
if self.units != other.units:
raise qnArrayTwoError('Units of the two array must be compatible')
return qnArrayTwo(super(qnArrayTwo, self).__sub__(other.value), units = self.units, sd = np.sqrt(self.sd**2+other.sd**2))
return qnArrayTwo(super(qnArrayTwo, self).__sub__(other), units = self.units, sd = self.sd)

def __rsub__(self, other):
if type(other) is qnArrayTwo:
if self.units != other.units:
raise qnArrayTwoError('Units of the two array must be compatible')
return qnArrayTwo(super(qnArrayTwo, self).__rsub__(super(qnArrayTwo, other)), units = self.units, sd = np.sqrt(self.sd**2+other.sd**2))
if type(other) is Quantity:
if self.units != other.units:
raise qnArrayTwoError('Units of the two array must be compatible')
return qnArrayTwo(super(qnArrayTwo, self).__rsub__(other.value), units = self.units, sd = np.sqrt(self.sd**2+other.sd**2))
return qnArrayTwo(super(qnArrayTwo, self).__rsub__(other), units = self.units, sd = self.sd)

def __mul__(self, other):
if type(other) is qnArrayTwo:
return qnArrayTwo(super(qnArrayTwo, self).__mul__(super(qnArrayTwo, other)), units = self.units * other.units)
if type(other) is Quantity:
return qnArrayTwo(super(qnArrayTwo, self).__mul__(other.value), units = self.units * other.units)
return qnArrayTwo(super(qnArrayTwo, self).__mul__(other), units = self.units)

def __rmul__(self, other):
return self.__mul__(other)

def __truediv__(self, other):
if type(other) is qnArrayTwo:
return qnArrayTwo(super(qnArrayTwo, self).__truediv__(super(qnArrayTwo, other)), units = self.units / other.units)
if type(other) is Quantity:
return qnArrayTwo(super(qnArrayTwo, self).__truediv__(other.value), units = self.units / other.units)
return qnArrayTwo(super(qnArrayTwo, self).__truediv__(other), units = self.units)

def __rtruediv__(self, other):
if type(other) is qnArrayTwo:
return qnArrayTwo(super(qnArrayTwo, self).__rtruediv__(super(qnArrayTwo, other)), units = other.units / self.units)
if type(other) is Quantity:
return qnArrayTwo(super(qnArrayTwo, self).__rtruediv__(other.value), units = other.units / self.units)
return qnArrayTwo(super(qnArrayTwo, self).__rtruediv__(other), units = Units('1') / self.units)

def __pow__(self, power):
return qnArrayTwo(super(qnArrayTwo, self).__pow__(power), units = self.units ** power)
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
if ufunc in ufunc_dict:
alg_func = ufunc_dict[ufunc]
# check for units matching
if hasattr(inputs[0],'units') and hasattr(inputs[1],'units'):
if inputs[0].units != inputs[1].units:
raise qnArrayTwoError('Units must match')
# if both are qn arrays
if (type(inputs[1]) is qnArrayTwo) and (type(inputs[0]) is qnArrayTwo):
return qnArrayTwo(getattr(np.asarray(inputs[0]), alg_func)(np.asarray(inputs[1])),
units = inputs[0].units,
sd = np.sqrt(inputs[0].sd**2+inputs[1].sd**2))
# if one input is a quantity
if type(inputs[1]) is Quantity:
return qnArrayTwo(getattr(np.asarray(inputs[0]), alg_func)(inputs[1].value),
units = inputs[0].units,
sd = np.sqrt(inputs[0].sd**2+inputs[1].sd**2))
if type(inputs[0]) is Quantity:
return qnArrayTwo(getattr(inputs[0].value, alg_func)(np.asarray(inputs[1])),
units = inputs[1].units,
sd = np.sqrt(inputs[1].sd**2+inputs[0].sd**2))
# for all other object types
if type(inputs[0]) is qnArrayTwo:
return qnArrayTwo(getattr(np.asarray(inputs[0]), alg_func)(inputs[1]),
units = inputs[0].units,
sd = inputs[0].sd)
else:
return qnArrayTwo(getattr(inputs[0], alg_func)(np.asarray(inputs[1])),
units = inputs[1].units,
sd = inputs[1].sd)

def __eq__(self, other):
if all(super(qnArrayTwo, self).__eq__(super(qnArrayTwo, other))) and (self.units == other.units):
@@ -111,3 +77,8 @@ def html_str(self):
for i in range(len(self)):
html_chunks.append('{} {}'.format(self[i],self.units))
return ', '.join(html_chunks)

ufunc_dict = { np.add: '__add__',
np.subtract: '__sub__',
np.multiply: '__mul__',
np.divide: '__truediv__'}