Hello all,
I'm having difficult writing code for a project in python where I need to filter through a bunch of vectors e.g. <x,y,z> by satisfying an equation.
I have a function of three variables (s, theta, phi), where the variables are just equally spaced numbers (np.linspace). This function just multiplies a bunch of matrices together and outputs more matrices that represent 3D coordinates as vectors. Given an equation which I have included a picture of, the output vectors/points need to satisfy this equation. How can I go about doing this efficiently? I have millions of these points since my s,theta, phi parameters are very finely spaced apart.
#π How do I efficiently filter/sort through vectors?
34 messages Β· Page 1 of 1 (latest)
@prisma kiln
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
def function(x: int, y: int, z: int) -> int:
return x + y + z
satisfiers = list(filter(function, vectors))```
The only way to do this more efficiently would be to write a C or Cython function and expose it to python - but I'm not sure how much more efficient that would be, because there's a conversion step
If your predicate is multi step or involves particularly complex operations, it might be worth it
There's also numpy
does the -> int mean that outputs should equal an int(e.g. 0 in this case)?
Yes - the arrow followed by a type indicates the return type
If your predicate (the function you're using) is just math operations then numpy is definitely going to be the fastest
ok
With numpy you feed the entire table of vectors into the computer at once, and use the GPU to compute them all simultaneously
i believe numpy has np.gradient which calculates derivatives for all variables
Oh, is that your operation?
yes, partial derivative with respect to s variables crossed (cross product) with partial derivative with respect to theta. and then the entire thing is dot product with partial derivative with respect to phi.
Me, realizing the last few years was just a dream, and I'm still studying for a calculus exam in the morning
I enjoyed calculus, and I'm glad I took it. But I'm also glad its over
i think ill just give it a try with numpy and see what happens. numpy already has np.cross and np.dot so im just gonna see how fast it does this.
If you can do it in numpy, do so. That'll be on the GPU and enormously faster. If you can't, try Cython. For several purely mathematical operations, it'll be faster than just pure pythom. You can look into using a pool of processes to perform multiple operations at once
There's probably some extreme hacks you can use if performance is critical, but if you're just looking a sane level of speed, the above is good enough
i did not know numpy ran natively on gpu
yea i tried using that once and just gave up lol
if numpy works ill leave it at that, if not ill try gpu computation
thanks for the help
I just looked it up by the way
Assuming a matrix of millions of purely numeric vectors, and a predicate composed of multiple purely mathematical operations (including cross and dot), torch would be the preferred method
thanks, will be sure to try that
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.