bugGNU Octave - Bugs: bug #65553, stream2 doesn't extend matrices...


bug #65553: stream2 doesn't extend matrices like matlab

Submitter:  Andy Adler <andy_adler>
Submitted:  Wed 03 Apr 2024 01:16:27 PM UTC
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Patch Submitted Assigned to:  None
Originator Name:  Andy Adler Open/Closed:  * Open
Release:  * 9.1.0 Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup

Sun 07 Apr 2024 07:28:49 AM UTC, comment #3: 

In the stream2 and stream3 functions, the "x,y" arguments are reduced to the vectors "gx,gy" anyway. Instead of expanding the input "x,y" to matrices to later reduce them back to vectors, I suggest changing the input validation so that depending on whether the input is a vector or a matrix, the correct data and format is copied to "gx,gy". If we do this, we also do not need to perform any numerical operations on input data before it is checked by the input validation. streamline.m also needs to be adjusted as it is not compatible with vectors.

Hg200 <hg200>
Fri 05 Apr 2024 06:44:05 AM UTC, comment #2: 

Thank you for the report. Two issues are addressed here. First topic is to specify the input data x, y as vectors instead of 2D mesh grids. It looks like Matlab has an undocumented feature here. The documentation says that x, y must be a 2D array with the same size as u, v. However, I like the idea of being able to specify x,y also as a vectors. The second issue is how to deal with reversed input data. The Matlab documentation only says "monotonic", but I haven't found anything about "ascending" or "descending". And it looks like Matlab also accepts reversed input data.

1.) If we add the "vector input" feature to Octave, we also need to add it in the stream3() function. Let's take the example from the Octave demo. The following stream3() code runs in Matlab, but not in Octave:

[x, y, z] = meshgrid (-1:0.4:1, -2:0.2:2, -3:0.3:0);
u = - 0.08 * x - y; v = x - 0.08 * y; w = - 0.04 * ones (size (x));
streamline (x(1, :, 1), y(:, 1, 1), squeeze (z(1, 1, :)), u, v, w, 1, 0, 0);
view (3); axis equal tight;

2.) I have doubts about the "doflip" proposal made in the original submission. If the input vector is reversed due to descending order, the code also reverses the vector field data. Matlab does not seem to do this. With the patch from the original submission, the following example results in different plot outputs between Octave and Matlab. The Matlab representation is correct, but Octave is wrong:

[x, y] = meshgrid (-5:5, -4:4);
x = -x;
u = x - 2 * y; v = 2 * x - 3 * y;
sx = [3, 0, -1, -2, -3, 0, 1, 2]; sy = [3, 3, 3, 3, -3, -3, -3, -3];
streamline (x(1,:), y(:,1), u, v, sx, sy, 0.05);
hold on;
quiver (x, y, u, v);
scatter (sx(:), sy(:), 20, "filled", "o", "markerfacecolor", "r");
grid on; axis equal;

I feel it is necessary to elaborate the dependencies in more detail. It is a long time ago, but if i remember correctly the Runge-Kutta integrator in stream-euler.cc should conceptually also be able to deal with descending input data. But i have to check this in detail.

Although both topics are linked in some way, my proposal is to split this report:
--> Feature request "x,y vector input instead of matrix input"
--> Compatibility with descending input data

Hg200 <hg200>
Wed 03 Apr 2024 03:12:13 PM UTC, comment #1: 

Thank you for the report and proposed solution.
I haven't looked at it in any detail yet.
CC'ing hg200 as the original author of that function in case he has any thoughts on the proposed change.

Markus M├╝tzel <mmuetzel>
Group administrator
Wed 03 Apr 2024 01:16:27 PM UTC, original submission:  

Sorry for the stream of reports. I'm working on a release of eidors.org and I'm testing matlab/octave compatibility.

In matlab, the following code works. In octave, the
issue is the error below:

xp = linspace(-2,2,200); yp = linspace(-3,3,150);
sx = linspace(-1,1,20); sy = linspace(-1,1,20);
[xc,yc]= meshgrid(xp,yp);
% error: stream2: matrix dimensions must match

This can be corrected with a function "fix_to_matrix" in the following patch:

share/octave/9.1.0/m/plot/draw$ diff -c stream2.m stream2-old.m
* 92,103 **
        [x, y] = meshgrid (1:n, 1:m);
      case 6
        [x, y, u, v, spx, spy] = varargin{:};
+       [x, u] = fix_to_matrix(x, u, 1);
+       [y, v] = fix_to_matrix(y, v, 2);
      case 7
        [x, y, u, v, spx, spy, options] = varargin{:};
+       [x, u] = fix_to_matrix(x, u, 1);
+       [y, v] = fix_to_matrix(y, v, 2);
        print_usage ();
--- 92,99 ----
* 187,223 **
+ # check if x is vector. Make full size
+ # axis (=1 for x and =2 for y)
+ function [x, u] = fix_to_matrix(x, u, axis);
+   if size_equal(x,u)
+     return
+   endif
+   if all(size(x)>1)
+     return
+   endif
+   dirn = diff( x );
+   if all(dirn>0)
+     doflip = false;
+   elseif all(dirn<0)
+     doflip = true;
+   else
+     error ("stream2: axis not monotonic");
+   end
+   # extend x in the rows
+   x = x(:).';
+   x = x(ones(size(u,axis),1),:);
+   if axis==2
+     x = x.';
+   endif
+   if doflip % flipud if axis=2, rl if axis=1
+     x = flip( x, 3-axis );
+     u = flip( u, 3-axis );
+   endif
+ endfunction
  %! clf;
--- 183,188 ----

Andy Adler <andy_adler>


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

Attach Files:

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 hg200 (Posted a comment)
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by mmuetzel
  • -email is unavailable- added by andy_adler (Submitted the item)

    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 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2024-04-04 rik5 StatusNone Patch Submitted
    2024-04-03 mmuetzel Carbon-Copy- Added hg200

    Back to the top

    Powered by Savane 3.13-4b48.
    Corresponding source code