bugGNU Octave - Bugs: bug #51728, del2 fails on 1-D input with a...

 
 

bug #51728: del2 fails on 1-D input with a vector as spacing

Submitter:  None
Submitted:  Fri 11 Aug 2017 11:04:56 AM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  Fixed Assigned to:  None
Originator Name:  Roger Jeurissen Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * 4.0.0
Operating System:  * GNU/Linux Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Fri 25 Aug 2017 11:29:33 PM UTC, comment #8: 

I fixed this on the development branch.  See cset http://hg.savannah.gnu.org/hgweb/octave/rev/c7b801f36be4.  Since this is just an m-file, the original reporter (OP) can grab the file del2.m from the Octave Mercurial repository to get the fix. 

The OP also mentioned a problem with circshift.  Please file a new bug report about that with an example of the problem.

Rik <rik5>
Group administrator
Fri 25 Aug 2017 06:11:51 AM UTC, comment #7: 

Never mind.  I found a bug in the code.  I don't need the test in comment #6 to be run.

Rik <rik5>
Group administrator
Fri 25 Aug 2017 05:31:31 AM UTC, comment #6: 

I've re-coded the input validation, but I need another test run.  What does Matlab do for this code


 a = zeros (9,9);
 a(5,5) = 1.0;
 b = 4*del2 (a,1,2);



Rik <rik5>
Group administrator
Thu 24 Aug 2017 09:23:31 AM UTC, comment #5: 

On Matlab R2017a:


> del2 (1:5, ones (5,5), 1)

ans =

   NaN   NaN   NaN   NaN   NaN

>> del2 (exp ([1:5]), 2, 5)

ans =

   -0.3603    0.5016    1.3635    3.7064    6.0493

>> xx = meshgrid (1:5)

xx =

     1     2     3     4     5
     1     2     3     4     5
     1     2     3     4     5
     1     2     3     4     5
     1     2     3     4     5

>> del2 (exp (xx), 2, 5)

ans =

   -0.3603    0.5016    1.3635    3.7064    6.0493
   -0.3603    0.5016    1.3635    3.7064    6.0493
   -0.3603    0.5016    1.3635    3.7064    6.0493
   -0.3603    0.5016    1.3635    3.7064    6.0493
   -0.3603    0.5016    1.3635    3.7064    6.0493


Guillaume <gyom>
Thu 17 Aug 2017 12:22:54 PM UTC, comment #4: 

The code snippet that I gave didn't come out as I expected. Here's a new attempt:

      if(nd==2&&xor(sz(1)==1,sz(2)==1)&&xor(sz(1)>1,sz(2)>1))
        h_new{sz>1}=1;
        h_new{sz==1}=h;
        D=del2(M,h_new{:});
        return
      endif


Roger Jeurissen <rogerjeurissen>
Thu 17 Aug 2017 12:19:39 PM UTC, comment #3: 

The Matlab documentation for del2 is indeed incorrect here. I said that the example code that I gave works in Matlab, because it came directly from the example on the Mathworks website. I assume that they checked their examples. I don't have access to Matlab right now.

Your suggestion of supplying a spacing for the y-direction is actually much simpler than what I proposed. I assumed that that wouldn't work because the doc says:

At least 3 data points are needed for each dimension.

This solution relies on what happens when there are fewer than 3 data points; that dimension is ignored, so it works. This could be implemented by adding this right after line 80 ( which is h = varargin{1}; )


if(nd==2&amp;amp;amp;amp;&amp;amp;amp;amp;xor(sz(1)==1,sz(2)==1)&amp;amp;amp;amp;&amp;amp;amp;amp;xor(sz(1)&amp;amp;amp;gt;1,sz(2)&amp;amp;amp;gt;1))
  h_new{sz&amp;amp;amp;gt;1}=1;
  h_new{sz==1}=h;
  D=del2(M,h_new{:});
  return
endif


I just noticed that circshift has the same issue.

Roger Jeurissen <rogerjeurissen>
Fri 11 Aug 2017 05:08:23 PM UTC, comment #2: 

Probably easiest to start a session and execute 'diary on'.  At the end of the session upload the diary file to the bug report?

Sample Code #1:


del2 (1:5, ones (5,5), 1)


Does this produce an error because the spacing is neither a scalar nor a vector?

Sample Code #2:


del2 (exp ([1:5]), 2, 5)


Documentation says the first spacing applies to the first dimension.  However, the first dimension is rows, not columns, which is different than specifying the x-separation which people think of as the first argument.

Sample Code #3:


xx = meshgrid (1:5)
del2 (exp (xx), 2, 5)


Just making sure they don't something special for the 1-D vectors in Code #2.


Rik <rik5>
Group administrator
Fri 11 Aug 2017 04:22:47 PM UTC, comment #1: 

Certainly Octave should be able to calculate this.

I think the issue is that Matlab defaults unspecified dimensions to a spacing of 1, despite the fact that the documentation says


 The number of spacing inputs must be equal to the number of dimensions in U.


For reference, see http://www.mathworks.com/help/matlab/ref/del2.html.

In Octave, the simple way to get this to work is to specify all dimensions.  In the Matlab language everything has a minimum of two dimensions.  1-D objects, vectors, are really just Nx1 or 1XN matrices.  I can get your code to work simply by supplying a spacing for the Y dimension.

Sample:


x = linspace(-2*pi,2*pi);
U = cos(x);
L = 4*del2(U,x, 1);


The m-file del2 hasn't been reviewed for a long time and probably needs an overhaul.  Given that you have access to Matlab, can you run a few sample pieces of code through Matlab and report the result?

Rik <rik5>
Group administrator
Fri 11 Aug 2017 11:04:56 AM UTC, original submission:  

According to the documentation, the discrete Laplacian of a 1-D array can be calculated with del2, with non-uniform spacing. When I tried to use this, octave threw an error:

error: del2: dimensionality mismatch in 1-th spacing vector

The following code reproduces the error:

x = linspace(-2*pi,2*pi);
U = cos(x);
L = 4*del2(U,x);


In Matlab, this does return a value without errors.

I suspect that this originates from line 73 of del2. There, ndims is used to obtain the number of dimensions of the input array, and this value is stored in the variable nd. However, ndims returns 2 for a 1-D array. What is needed is the number of non-singleton dimensions:


nd = sum(size(M)>1);


However, this would break the functionality later on, because the for loops run over the dimensions. To fix that, we can add the following lines after line 73:


if(nd==1)
   M=M(:);
end


We should also add this line at the end:


if(nd==1)
   D=reshape(D,size(M));
end


As far as I can see right now, this should work. I tested this fix to check that it solves the issue that i encountered, and it does.


Anonymous

 

(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 gyom (Posted a comment)
  • -email is unavailable- added by rogerjeurissen (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-08-25 rik5 StatusNeed Info Fixed
        Open/ClosedOpen Closed
    2017-08-15 rik5 StatusConfirmed Need Info
    2017-08-11 rik5 StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code