Giter Site home page Giter Site logo

Problem with vectorization about quadpy HOT 6 CLOSED

poncev avatar poncev commented on June 2, 2024
Problem with vectorization

from quadpy.

Comments (6)

poncev avatar poncev commented on June 2, 2024

I have zeroed in on the first point where the integrate method fails. During the evaluation of the function I want to integrate, I compute a matrix. Let us say t is one of the arguments, a real number, but scheme.integrate passes an array of points of integration, let us say t = [t_0, ..., t_7]. Then I define matrix similar to

R = np.array([[1, 0, 0],
              [0, np.cos(t), np.sin(t)],
              [0, -np.sin(t), np.cos(t)]])

Now, since t is an array, not a real value as I intended, the final matrix is misshaped, we get

array([1, 0, 0],
      [0, array([cos(t_0), ..., cos(t_7)]), array([sin(t_0), ..., sin(t_7)])], 
      ...)

Since the function to integrate has many more terms like this, I wonder whether there is a method to fix this kind of problems efficiently.

Thank you!

from quadpy.

nschloe avatar nschloe commented on June 2, 2024

I get the error: ValueError: shapes (2,28) and (2,) not aligned: 28 (dim 1) != 2 (dim 0)

Well, the error is in your own code. The line

    w = np.inner(v, np.array([1, 0]))

fails.

I don't understand your second post. You're wondering why quadpy gives you multiple input points?

from quadpy.

poncev avatar poncev commented on June 2, 2024

Thank you for replying. I also posted the question in stackoverflow; hopefully, it is more clear there. I'm not asking why quadpy gives multiple input points.

The function in my first post is f(x, y) = cos(y). The function works as expected if I pass [x_0, x_1], but I wanted to ilustrate the problems that may arise in the library. I admit that the example is artificial. I know that that line fails in quadpy, but it does not fail when you use the function as originally intended, with two real numbers.

In my second post I wrote a more realistic example. When I defined the function, I was thinking in a matrix with real valued entries (I didn't know I would use quadpy). Quadpy inputs a vector, so the matrix has mixed vector and real valued entries, and when I try to multiply this matrix with other marices or vectors, numpy does not know how to deal with the situation and throws errors. When I input a real value everything goes well.

I want to learn how I should define my functions so that they are compatible with quadpy. I have been able to integrate basic functions with quadpy, but when my functions entail matrices and operations on vectors, everything crashes For example, in my code, should I replace constant*vector by np.tensordot(constant, vector)? The problem is that quadpy replaces the constant by a vector, and the vector by an array of arrays.

I suppose that if I learn how to write vectorized versions of my functions, then the code will be more efficient.

from quadpy.

nschloe avatar nschloe commented on June 2, 2024

Perhaps this helps:

https://github.com/sigma-py/quadpy/wiki/Dimensionality-of-input-and-output-arrays

from quadpy.

poncev avatar poncev commented on June 2, 2024

Thank you! I will check next week.

from quadpy.

nschloe avatar nschloe commented on June 2, 2024

Closing for lack of feedback. Feel free to reopen at any time!

from quadpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.