Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ift
NIFTy
Commits
b3048ad6
Commit
b3048ad6
authored
Apr 25, 2018
by
Martin Reinecke
Browse files
shorten operator overloading etc.
parent
11a14c84
Pipeline
#27982
passed with stages
in 16 minutes and 34 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
nifty4/data_objects/distributed_do.py
View file @
b3048ad6
...
@@ -19,6 +19,7 @@
...
@@ -19,6 +19,7 @@
import
numpy
as
np
import
numpy
as
np
from
.random
import
Random
from
.random
import
Random
from
mpi4py
import
MPI
from
mpi4py
import
MPI
import
sys
_comm
=
MPI
.
COMM_WORLD
_comm
=
MPI
.
COMM_WORLD
ntask
=
_comm
.
Get_size
()
ntask
=
_comm
.
Get_size
()
...
@@ -185,75 +186,6 @@ class data_object(object):
...
@@ -185,75 +186,6 @@ class data_object(object):
else
:
else
:
return
data_object
(
self
.
_shape
,
tval
,
self
.
_distaxis
)
return
data_object
(
self
.
_shape
,
tval
,
self
.
_distaxis
)
def
__add__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__add__'
)
def
__radd__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__radd__'
)
def
__iadd__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__iadd__'
)
def
__sub__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__sub__'
)
def
__rsub__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rsub__'
)
def
__isub__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__isub__'
)
def
__mul__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__mul__'
)
def
__rmul__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rmul__'
)
def
__imul__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__imul__'
)
def
__div__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__div__'
)
def
__rdiv__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rdiv__'
)
def
__idiv__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__idiv__'
)
def
__truediv__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__truediv__'
)
def
__rtruediv__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rtruediv__'
)
def
__pow__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__pow__'
)
def
__rpow__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rpow__'
)
def
__ipow__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__ipow__'
)
def
__lt__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__lt__'
)
def
__le__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__le__'
)
def
__ne__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__ne__'
)
def
__eq__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__eq__'
)
def
__ge__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__ge__'
)
def
__gt__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__gt__'
)
def
__neg__
(
self
):
def
__neg__
(
self
):
return
data_object
(
self
.
_shape
,
-
self
.
_data
,
self
.
_distaxis
)
return
data_object
(
self
.
_shape
,
-
self
.
_data
,
self
.
_distaxis
)
...
@@ -269,6 +201,20 @@ class data_object(object):
...
@@ -269,6 +201,20 @@ class data_object(object):
def
fill
(
self
,
value
):
def
fill
(
self
,
value
):
self
.
_data
.
fill
(
value
)
self
.
_data
.
fill
(
value
)
for
op
in
[
"__add__"
,
"__radd__"
,
"__iadd__"
,
"__sub__"
,
"__rsub__"
,
"__isub__"
,
"__mul__"
,
"__rmul__"
,
"__imul__"
,
"__div__"
,
"__rdiv__"
,
"__idiv__"
,
"__truediv__"
,
"__rtruediv__"
,
"__itruediv__"
,
"__floordiv__"
,
"__rfloordiv__"
,
"__ifloordiv__"
,
"__pow__"
,
"__rpow__"
,
"__ipow__"
,
"__lt__"
,
"__le__"
,
"__gt__"
,
"__ge__"
,
"__eq__"
,
"__ne__"
]:
def
func
(
op
):
def
func2
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
op
)
return
func2
setattr
(
data_object
,
op
,
func
(
op
))
def
full
(
shape
,
fill_value
,
dtype
=
None
,
distaxis
=
0
):
def
full
(
shape
,
fill_value
,
dtype
=
None
,
distaxis
=
0
):
return
data_object
(
shape
,
np
.
full
(
local_shape
(
shape
,
distaxis
),
return
data_object
(
shape
,
np
.
full
(
local_shape
(
shape
,
distaxis
),
...
@@ -302,6 +248,7 @@ def vdot(a, b):
...
@@ -302,6 +248,7 @@ def vdot(a, b):
def
_math_helper
(
x
,
function
,
out
):
def
_math_helper
(
x
,
function
,
out
):
function
=
getattr
(
np
,
function
)
if
out
is
not
None
:
if
out
is
not
None
:
function
(
x
.
_data
,
out
=
out
.
_data
)
function
(
x
.
_data
,
out
=
out
.
_data
)
return
out
return
out
...
@@ -309,24 +256,14 @@ def _math_helper(x, function, out):
...
@@ -309,24 +256,14 @@ def _math_helper(x, function, out):
return
data_object
(
x
.
shape
,
function
(
x
.
_data
),
x
.
_distaxis
)
return
data_object
(
x
.
shape
,
function
(
x
.
_data
),
x
.
_distaxis
)
def
abs
(
a
,
out
=
None
):
_current_module
=
sys
.
modules
[
__name__
]
return
_math_helper
(
a
,
np
.
abs
,
out
)
def
exp
(
a
,
out
=
None
):
return
_math_helper
(
a
,
np
.
exp
,
out
)
def
log
(
a
,
out
=
None
):
return
_math_helper
(
a
,
np
.
log
,
out
)
def
tanh
(
a
,
out
=
None
):
return
_math_helper
(
a
,
np
.
tanh
,
out
)
def
sqrt
(
a
,
out
=
None
):
for
f
in
[
"sqrt"
,
"exp"
,
"log"
,
"tanh"
,
"conjugate"
,
"abs"
]:
return
_math_helper
(
a
,
np
.
sqrt
,
out
)
def
func
(
f
):
def
func2
(
x
,
out
=
None
):
return
_math_helper
(
x
,
f
,
out
)
return
func2
setattr
(
_current_module
,
f
,
func
(
f
))
def
from_object
(
object
,
dtype
,
copy
,
set_locked
):
def
from_object
(
object
,
dtype
,
copy
,
set_locked
):
...
...
nifty4/field.py
View file @
b3048ad6
...
@@ -23,6 +23,7 @@ from . import utilities
...
@@ -23,6 +23,7 @@ from . import utilities
from
.domain_tuple
import
DomainTuple
from
.domain_tuple
import
DomainTuple
from
functools
import
reduce
from
functools
import
reduce
from
.
import
dobj
from
.
import
dobj
import
sys
__all__
=
[
"Field"
,
"sqrt"
,
"exp"
,
"log"
,
"conjugate"
]
__all__
=
[
"Field"
,
"sqrt"
,
"exp"
,
"log"
,
"conjugate"
]
...
@@ -759,75 +760,6 @@ class Field(object):
...
@@ -759,75 +760,6 @@ class Field(object):
return
NotImplemented
return
NotImplemented
def
__add__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__add__'
)
def
__radd__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__radd__'
)
def
__iadd__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__iadd__'
)
def
__sub__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__sub__'
)
def
__rsub__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rsub__'
)
def
__isub__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__isub__'
)
def
__mul__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__mul__'
)
def
__rmul__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rmul__'
)
def
__imul__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__imul__'
)
def
__div__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__div__'
)
def
__truediv__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__truediv__'
)
def
__rdiv__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rdiv__'
)
def
__rtruediv__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rtruediv__'
)
def
__idiv__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__idiv__'
)
def
__pow__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__pow__'
)
def
__rpow__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__rpow__'
)
def
__ipow__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__ipow__'
)
def
__lt__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__lt__'
)
def
__le__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__le__'
)
def
__ne__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__ne__'
)
def
__eq__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__eq__'
)
def
__ge__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__ge__'
)
def
__gt__
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
'__gt__'
)
def
__repr__
(
self
):
def
__repr__
(
self
):
return
"<nifty4.Field>"
return
"<nifty4.Field>"
...
@@ -836,10 +768,25 @@ class Field(object):
...
@@ -836,10 +768,25 @@ class Field(object):
self
.
_domain
.
__str__
()
+
\
self
.
_domain
.
__str__
()
+
\
"
\n
- val = "
+
repr
(
self
.
val
)
"
\n
- val = "
+
repr
(
self
.
val
)
for
op
in
[
"__add__"
,
"__radd__"
,
"__iadd__"
,
"__sub__"
,
"__rsub__"
,
"__isub__"
,
"__mul__"
,
"__rmul__"
,
"__imul__"
,
"__div__"
,
"__rdiv__"
,
"__idiv__"
,
"__truediv__"
,
"__rtruediv__"
,
"__itruediv__"
,
"__floordiv__"
,
"__rfloordiv__"
,
"__ifloordiv__"
,
"__pow__"
,
"__rpow__"
,
"__ipow__"
,
"__lt__"
,
"__le__"
,
"__gt__"
,
"__ge__"
,
"__eq__"
,
"__ne__"
]:
def
func
(
op
):
def
func2
(
self
,
other
):
return
self
.
_binary_helper
(
other
,
op
=
op
)
return
func2
setattr
(
Field
,
op
,
func
(
op
))
# Arithmetic functions working on Fields
# Arithmetic functions working on Fields
def
_math_helper
(
x
,
function
,
out
):
def
_math_helper
(
x
,
function
,
out
):
function
=
getattr
(
dobj
,
function
)
if
not
isinstance
(
x
,
Field
):
if
not
isinstance
(
x
,
Field
):
raise
TypeError
(
"This function only accepts Field objects."
)
raise
TypeError
(
"This function only accepts Field objects."
)
if
out
is
not
None
:
if
out
is
not
None
:
...
@@ -850,22 +797,11 @@ def _math_helper(x, function, out):
...
@@ -850,22 +797,11 @@ def _math_helper(x, function, out):
else
:
else
:
return
Field
(
domain
=
x
.
_domain
,
val
=
function
(
x
.
val
))
return
Field
(
domain
=
x
.
_domain
,
val
=
function
(
x
.
val
))
_current_module
=
sys
.
modules
[
__name__
]
def
sqrt
(
x
,
out
=
None
):
for
f
in
[
"sqrt"
,
"exp"
,
"log"
,
"tanh"
,
"conjugate"
]:
return
_math_helper
(
x
,
dobj
.
sqrt
,
out
)
def
func
(
f
):
def
func2
(
x
,
out
=
None
):
return
_math_helper
(
x
,
f
,
out
)
def
exp
(
x
,
out
=
None
):
return
func2
return
_math_helper
(
x
,
dobj
.
exp
,
out
)
setattr
(
_current_module
,
f
,
func
(
f
))
def
log
(
x
,
out
=
None
):
return
_math_helper
(
x
,
dobj
.
log
,
out
)
def
tanh
(
x
,
out
=
None
):
return
_math_helper
(
x
,
dobj
.
tanh
,
out
)
def
conjugate
(
x
,
out
=
None
):
return
_math_helper
(
x
,
dobj
.
conjugate
,
out
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment