Skip to content

add flatten() method and test#112

Open
arsenovic wants to merge 2 commits intotBuLi:masterfrom
arsenovic:arsenovic_flatten
Open

add flatten() method and test#112
arsenovic wants to merge 2 commits intotBuLi:masterfrom
arsenovic:arsenovic_flatten

Conversation

@arsenovic
Copy link
Collaborator

@arsenovic arsenovic commented Jan 1, 2026

i am not sure the correct way to do this in full generallity, but it was helpful for plotting arrays-of mv's.

Copy link
Owner

@tBuLi tBuLi left a comment

Choose a reason for hiding this comment

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

Thanks for this PR! Definitelly something we want to have. I just have some comments.


# Get shapes of all values
shapes = []
for v in self._values:
Copy link
Owner

Choose a reason for hiding this comment

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

I would prefer a list comprehension, one concept one line.


# Broadcast each value to the common shape
broadcasted_values = []
for v in self._values:
Copy link
Owner

Choose a reason for hiding this comment

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

also a list comprehension.


return self.fromkeysvalues(self.algebra, keys=self._keys, values=broadcasted_values)

def flatten(self):
Copy link
Owner

Choose a reason for hiding this comment

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

This should still return a multivector. For example, for a multivector of shape (4, N, M) I would expect flatten to return a multivector of shape (4, M*N). Since multivectors are still iterable there is no difference when using flatten for loops but this also still allows you to do GA operatons on it efficiently.

Moreover, the datatype of _values for the new array should be the same of the old one, when possible.

:return: List of flattened multivectors.
"""
mv = self.broadcast()
return [mv[i] for i in np.ndindex(mv.shape[1:])]
Copy link
Owner

@tBuLi tBuLi Jan 29, 2026

Choose a reason for hiding this comment

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

I would rename i to indices because i suggests a single int.

Moreover, we can easily add a depth argument to this method, which should default to a full flatten like numpy does:

    def flatten(self, depth=-1):
        mv = self.broadcast()
        return [mv[i] for i in np.ndindex(mv.shape[1:(len(mv.shape) if depth is None else depth+1)])]

Also add a test for this, I might have an of by one error :P.

This in addition to the previous comment, so this function will still have to change more.


def test_flatten():
alg = Algebra(4)
shape = (len(tuple(alg.indices_for_grade(2))), 3, 4)
Copy link
Owner

Choose a reason for hiding this comment

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

I dn't think you need to cast to tuple to get the right len?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants