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

dsl: Add functionality to intersect SubDimensions #2222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions devito/types/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,65 @@ def middle(cls, name, parent, thickness_left, thickness_right, local=False):
thickness=((lst, thickness_left), (rst, thickness_right)),
local=local)

@classmethod
def intersection(cls, sdims):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not be a class method, similar to Set that defines intersection as set().intersectio not Set.intersection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it wants to be a class method since you could conceivably want the intersection of several SubDimensions, so it would want to be intersect(subdimensions) rather than intersect(self, other) imo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still have multiple ones, again similar to a set set(a).intersection(a,b,c,d,e,f) there is nothing that prevents it. to have more than one without being a class method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but then you have to arbitrarily pick one to be the "important" one with which you are intersecting all the others

# Check all SubDimensions supplied overlap
# NOTE: Think the test for overlap isn't strict enough?
if not all([sdims[0].overlap(sdim) for sdim in sdims[1:]]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would make it easier here to with just self.overlap ...

raise ValueError("SubDimensions do not all overlap")

name = '_n_'.join([sdim.name for sdim in sdims])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_i_ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was thinking it looks like the set intersection symbol if you squint at it?

try:
[parent] = set([sdim.parent for sdim in sdims])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{self.parent}.union(*[sdim.parent for sdim in sdims])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is both getting the parent and checking they all share a parent, since the unpack will fail with ValueError if the set does not contain one item

except ValueError:
raise ValueError("Inconsistent SubDimension parents")

local = any([sdim.local for sdim in sdims])

if all([sdim.is_left for sdim in sdims]):
# Only left SubDimensions results in left SubDimension
thickness = sympy.Min(*[sdim.thickness.left[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use our Min not sympy

for sdim in sdims])
return cls.left(name, parent,
thickness,
local=local)
elif all([sdim.is_right for sdim in sdims]):
# Only right SubDimensions results in right SubDimension
thickness = sympy.Min(*[sdim.thickness.right[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, devito.Min

for sdim in sdims])
return cls.right(name, parent,
thickness,
local=local)
else:
# Intersection of different types of subdimension or two middle
# will always be middle
width = parent.symbolic_max - parent.symbolic_min + 1

def get_left_thickness(sdim):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define as instance attribute outside, and then just self/sdim.right_thickness , no need for "get"

if sdim.is_left:
return 0
elif sdim.is_right:
return width - sdim.thickness.right[1]
else:
return sdim.thickness.left[1]

def get_right_thickness(sdim):
if sdim.is_left:
return width - sdim.thickness.left[1]
elif sdim.is_right:
return 0
else:
return sdim.thickness.right[1]

thickness_left = sympy.Max(*[get_left_thickness(sdim)
for sdim in sdims])
thickness_right = sympy.Max(*[get_right_thickness(sdim)
for sdim in sdims])
return cls.middle(name, parent,
thickness_left,
thickness_right,
local=local)

@cached_property
def symbolic_min(self):
return self._interval.left
Expand Down