bugGNU Octave - Bugs: bug #59403, integral2 and quad2d functions do...

 
 

bug #59403: integral2 and quad2d functions do not accurately document form of integrand function F

Submitted by:  Luis Kao <kogiokka>
Submitted on:  Tue 03 Nov 2020 06:15:14 AM UTC  
 
Category:  Documentation Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Inaccurate Result
Status:  Confirmed Assigned to:  None
Originator Name:  Luis Kao Open/Closed:  Open
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

( Jump to the original submission)

Sat 14 Nov 2020 03:00:45 PM UTC, comment #10: 

comment #9:

> We can't base the implementation of Octave on any information obtained from looking at parts of Matlab that might be available.  If you value the existence of Octave, please do not peek at and share information about the internals of Matlab here.


I'm sorry. It won't happen again.

Luis Kao <kogiokka>
Sat 14 Nov 2020 02:12:22 PM UTC, comment #9: 

We can't base the implementation of Octave on any information obtained from looking at parts of Matlab that might be available.  If you value the existence of Octave, please do not peek at and share information about the internals of Matlab here.

John W. Eaton <jwe>
Project Administrator
Sat 14 Nov 2020 09:05:21 AM UTC, comment #8: 

comment #7:

> ... The comment states that it would make it harder for the user "generating an error by using matrix functions instead of elementwise functions". ...


EDIT:
otherwise it would make it harder for the user

Luis Kao <kogiokka>
Sat 14 Nov 2020 09:00:34 AM UTC, comment #7: 

So I took a peek at quad2d in MATLAB. It does check if "FUN is properly vectorized". The comment states that it would make it harder for the user "generating an error by using matrix functions instead of elementwise functions". I don't know if there is a reason using matrix function within integration, but MATLAB seems to suggest users should not do that.

Also there is another thing they explicitly check about size mismatch:

quad2d(@(x,y) 1, 0, 1, 0, 1)

Octave returns value 1, while MATLAB throws an error: "Integrand output size does not match the input size."

Luis Kao <kogiokka>
Thu 12 Nov 2020 02:03:10 PM UTC, comment #6: 

Matlab uses a G3,K7 Guass-Konrad rule and Octave a G7,K15 Guass-Konrad rule in quad2d. So the only difference is the matlab should call the integrand with a 7x7 matrix of scalar arguments and Octave with a 15x15 matrix of scalar arguments. So matlab is detecting the issue somehow. Perhaps they do something like

try
   f(ones(2,1),ones(2,1)
catch
   error('ERROR HERE')
endcatch

To detect matrix cases before the rest of quad2d. But if they are then calling the integrand better not have any side effects

D.

David Bateman <dbateman>
Project Member
Thu 05 Nov 2020 05:01:20 PM UTC, comment #5: 

This helps make my argument.  The documentation for the form of the integrand function is misleading.  It used to be the case that F had to take the very particular form mentioned in integral2/quad2d because of the way 2-D integration was done.  This is no longer the case, but you can still see a bit of the history of the function in that there is a "method" property that can take the value "iterated" or "tiled".  For "iterated", F used to have that specific form.

Rik <rik5>
Project Administrator
Thu 05 Nov 2020 04:25:08 PM UTC, comment #4: 

After seeing the comments and checking the documentation again, I would like to provide some extra arguments.


If the user's intention is to do matrix multiplication, they will expect a matrix output. For example

function z = f(x,y)
  z = sinc(x) * sinc(y);
endfunction

x = [1 2; 3 4];
y = [5 6; 7 8];

f(x, y)
    =>   2.0836e-49  -3.7300e-50
         2.0836e-49  -3.7300e-50

And since that's the form of the original function, I suspect that they would want a matrix output from integral2/quad2d as well.

Another thing is that the documentation says

>     F is a function handle, inline function, or string containing the
>     name of the function to evaluate.  The function F must be of the
>     form z = f(x,y) where X is a vector and Y is a scalar.  It should
>     return a vector of the same length and orientation as X.


I don't know the implementation reason why Y should be a scalar, but it seems like current behaviors of integral2/quad2d disagree with the documentation.

function z = f(x,y)
  disp("Y isscalar:");
  isscalar(y)
  z = sinc(x) * sinc(y);
endfunction

integral2(@f, -1, 1, -1, 1);

The result from the script above is

Y isscalar:
ans = 0
Y isscalar:
ans = 0
Y isscalar:
ans = 0
Y isscalar:
ans = 0

Luis Kao <kogiokka>
Thu 05 Nov 2020 04:20:31 PM UTC, comment #3: 

This is what I suspected.  It was more or less luck, an artifact of how Matlab implements integral2, that led to the warning.  In Octave, I think the underlying implementation is trying for better performance so instead of calling the integrand function 15x15 = 225 times and incurring the overhead of a function call it calls the integrand once with a meshgrid 15x15 input.

Note, that Matlab's own documentation says the integrand function must use element-wise operators.

Integrand, specified as a function handle, defines the function to be integrated over the planar region xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x). The function fun must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations.

My leaning is to treat this bug as a documentation issue and more specifically mention that the function must return an array and use element-wise operators.

Rik <rik5>
Project Administrator
Tue 03 Nov 2020 05:52:41 PM UTC, comment #2: 

I changed the example to be

function r = fxy (x, y)
  size (x), size (y),
  r = sinc (x) * sinc (y);
end
integral2 (@fxy, -1, 1, -1, 1)

and I see

ans =

   15   15

ans =

   15   15

ans =

   15   15

ans =

   15   15

ans =

   15   15

ans =

   15   15

ans =

   15   15

ans =

   15   15

ans = 12.328

So, unfortunately, the integration function has selected X, Y values that do conform for matrix multiplication.  So I don't see how we would detect an error here.

John W. Eaton <jwe>
Project Administrator
Tue 03 Nov 2020 05:31:37 PM UTC, comment #1: 

Confirmed.  Changing release to "dev" as any fix is likely to occur on the development branch so there is enough time to thoroughly test a solution before release.

The solution may be more documentation.  Both functions are valid and therefore a user might have wanted to integrate the first one, even if it does seem like an odd choice.

Another possibility, if the condition can be detected easily, would be to emit a warning rather than an error so the user would have the chance to check whether the integrand function was really the one they intended.

Rik <rik5>
Project Administrator
Tue 03 Nov 2020 06:15:14 AM UTC, original submission:  

Octave silently produces an "incorrect" result when the input function F does not use element-by-element multiplication. This behavior is different from MATLAB, which throws the error message:

```
Error using  *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
```

Example Script

f = @(x, y) sinc(x) * sinc(y);
integral2(f, -1, 1, -1, 1)
    => 12.32805347760951

f = @(x, y) sinc(x) .* sinc(y);
integral2(f, -1, 1, -1, 1)
    => 1.389993237875656

Luis Kao <kogiokka>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by dbateman (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by kogiokka (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-11-05 rik5 CategoryOctave Function => Documentation
        Item GroupMissed Error or Warning => Inaccurate Result
        Operating SystemGNU/Linux => Any
        Summaryintegral2 and quad2d functions do not catch incorrect dimensions for matrix multiplication => integral2 and quad2d functions do not accurately document form of integrand function F
    2020-11-03 rik5 StatusNone => Confirmed
        Release5.2.0 => dev

    Back to the top


    Powered by Savane 3.5