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

add helper method to concatenate vectors #36971

Merged
merged 3 commits into from
Mar 25, 2024

Conversation

yyyyx4
Copy link
Member

@yyyyx4 yyyyx4 commented Dec 26, 2023

Writing vector(R, list(v) + list(w)) is a common way to concatenate two vectors v and w over some ring R.

In this patch we introduce a simple helper method which allows writing v.concatenate(w) instead.

Copy link
Contributor

@RuchitJagodara RuchitJagodara left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@dcoudert
Copy link
Contributor

Have you tried something like

from itertools import chain
return vector(R, chain(self, other))

It might be faster. At least it could be tested.

@mantepse
Copy link
Collaborator

Are you sure we want to have this?

@yyyyx4
Copy link
Member Author

yyyyx4 commented Dec 27, 2023

@dcoudert Tested it just now for vectors up to length one million; there seems to be essentially no difference in timings.
@mantepse I personally use this operation quite frequently. Why would we not want to have it?

@videlec
Copy link
Contributor

videlec commented Dec 28, 2023

The checks are not enough for other. Having the same base ring is not enough (it could be for example a polynomial).

@mantepse
Copy link
Collaborator

Why don't you allow any iterable as other? Put differently: why don't you omit the checks completely?

I think this would be more practical. Is there any danger of having a result that is invalid but would not raise an error?

@videlec
Copy link
Contributor

videlec commented Dec 29, 2023

Why don't you allow any iterable as other? Put differently: why don't you omit the checks completely?

I think this would be more practical. Is there any danger of having a result that is invalid but would not raise an error?

A good reason to have strict specifications is that this could be optimized a lot in subclasses.

@mantepse
Copy link
Collaborator

Could you explain how this helps precisely? Or rather, why omitting the checks would make optimisation difficult?

@videlec
Copy link
Contributor

videlec commented Dec 29, 2023

Could you explain how this helps precisely? Or rather, why omitting the checks would make optimisation difficult?

If you know that other is a free module of the same type, you can use Cython to do something like

new_vector = FastVectorConstruction(self._length + other._length)
memcpy(new_vector._data, self._data, self._length * sizeof(mpz_t))
memcpy(new_vector._data + self._data, other._data, other._length * sizeof(mpz_t))
return new_vector

@videlec
Copy link
Contributor

videlec commented Dec 29, 2023

Checking that two vectors have the same parent can be done efficiently, but checking that two vectors only differ by their dimension is trickier. As far as I know, the sage coercion system is not smart enough for that.

@mantepse
Copy link
Collaborator

Could you explain how this helps precisely? Or rather, why omitting the checks would make optimisation difficult?

If you know that other is a free module of the same type, you can use Cython to do something like

new_vector = FastVectorConstruction(self._length + other._length)
memcpy(new_vector._data, self._data, self._length * sizeof(mpz_t))
memcpy(new_vector._data + self._data, other._data, other._length * sizeof(mpz_t))
return new_vector

Yes, but couldn't you do the check in the subclasses you wanted to optimise? (Please excuse my possibly naive questions!)

@tscrim
Copy link
Collaborator

tscrim commented Jan 5, 2024

I agree with @mantepse about having the general case be as general as possible. Subclasses will have to implement their own version for vectors of the same class, which could then punt up to the base class when the type doesn't match.

@yyyyx4
Copy link
Member Author

yyyyx4 commented Jan 13, 2024

Thanks everyone for the feedback; I gave it another try.

Copy link

Documentation preview for this PR (built with commit 4d1f385; changes) is ready! 🎉

Copy link
Contributor

@grhkm21 grhkm21 left a comment

Choose a reason for hiding this comment

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

It says this branch is out-of-date with the base branch, which I assume means it can be merged without conflicts.

The changes look good to me.

@vbraun vbraun merged commit 3ea5fa9 into sagemath:develop Mar 25, 2024
24 checks passed
@mkoeppe mkoeppe added this to the sage-10.4 milestone Mar 25, 2024
@yyyyx4 yyyyx4 deleted the public/concatenate_vectors branch October 27, 2024 00:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants