# bug #48564: Octave does not accept "1 / ND array", "mrdivide (1, ND array)", "mldivide (ND array, 1)"

 Submitted by: Philip Nienhuis Submitted on: Tue 19 Jul 2016 09:03:29 PM UTC Category: Octave Function Severity: 3 - Normal Priority: 5 - Normal Item Group: Matlab Compatibility Status: Patch Submitted Assigned to: None Originator Name: philipnienhuis Open/Closed: Open Release: dev Operating System: Any

You are not logged in

## Discussion

Sun 29 Jan 2017 08:14:21 PM UTC, comment #9:

Admittedly I've been including this patch in my mxe cross-builds ever since July '16 but never came back to this bug report.

JWE noted additional tests are required as well.

AFAIR mldivide's behavior should still be patched as well, see comment #7. I just checked this in Matlab r2017a prerelease:

>> format compact
>> A = rand (2, 2)
A =
0.9572    0.8003
0.4854    0.1419
>> 1/A
Error using  /
Matrix dimensions must agree.
>> mrdivide (1, A)
Error using  /
Matrix dimensions must agree.
>> mldivide (A, 1)
Error using  \
Matrix dimensions must agree.
>>
>> B = rand (2, 2, 2)
B(:,:,1) =
0.4218    0.7922
0.9157    0.9595
B(:,:,2) =
0.6557    0.8491
0.0357    0.9340
>> 1/B
ans(:,:,1) =
2.3710    1.2623
1.0920    1.0422
ans(:,:,2) =
1.5250    1.1777
28.0020    1.0707
>> mrdivide (1, B)
ans(:,:,1) =
2.3710    1.2623
1.0920    1.0422
ans(:,:,2) =
1.5250    1.1777
28.0020    1.0707
>> mldivide (B, 1)
ans(:,:,1) =
2.3710    1.2623
1.0920    1.0422
ans(:,:,2) =
1.5250    1.1777
28.0020    1.0707
>>

..while Octave (with John's patch) does:

>> format compact
>> A = rand (2,2 )
A =
0.0056131   0.2872653
0.2929287   0.0012764

>> 1/A
ans =
178.1534     3.4811
3.4138   783.4536

>> mrdivide (1, A)
ans =
178.1534     3.4811
3.4138   783.4536

>> mldivide (A, 1)
error: mldivide: operator \: nonconformant arguments (op1 is 2x2, op2 is 1x1)
>>
>> B= rand(2,2,2)
B =
ans(:,:,1) =
0.81579   0.11887
0.17132   0.96621
ans(:,:,2) =
0.042480   0.204788
0.486795   0.743011

>> 1/B
ans =
ans(:,:,1) =
1.2258   8.4122
5.8371   1.0350
ans(:,:,2) =
23.5404    4.8831
2.0543    1.3459

>> mrdivide (1, B)
ans =
ans(:,:,1) =
1.2258   8.4122
5.8371   1.0350
ans(:,:,2) =
23.5404    4.8831
2.0543    1.3459

>> mldivide (B, 1)
error: mldivide: operator \: nonconformant arguments (op1 is 2x4, op2 is 1x1)
>>

(BTW where do those empty lines in Octave come from? they started appearing some months ago)

Philip Nienhuis <philipnienhuis>
Sun 29 Jan 2017 07:35:54 PM UTC, comment #8:

Ping.  This report was mostly solved and has a patch attached.  It seems like documentation could be added and then the patch committed.  Or, the patch could be changed to validate the dimensions rather than just the number of elements (see comment #7) and then committed.

Rik <rik5>
Wed 20 Jul 2016 06:41:44 PM UTC, comment #7:

Patch works fine in Linux now, but indeed, when A is any 2D array, patched Octave will accepts constructs like

1 / A
-and-
mrdivide (1, A)

that Matlab doesn't accept. So I guess merely checking for number of dimensions should do the trick.

mldivide won't accept (A, 1), nor (if B = 3D array) (B, 1). The former should be OK, the latter not.

Philip Nienhuis <philipnienhuis>
Wed 20 Jul 2016 02:34:11 PM UTC, comment #6:

If we consider Matlab as "authorative"....
Matlab won't accept any 2D matrix as divisor as in "1 / rand (2, 3)" or "mrdivide (1, rand 3, 2))"

Philip Nienhuis <philipnienhuis>
Wed 20 Jul 2016 01:01:29 PM UTC, comment #5:

Oh, but maybe the test is not correct.  Should it just be a test on the number of dimensions, or should element-by-element division also be done if the matrix is 2-dimensional but not square?

Also, this behavior should be documented.

John W. Eaton <jwe>
Wed 20 Jul 2016 12:58:25 PM UTC, comment #4:

I think the attached patch should solve this problem.  We could use tests for each combination of types (real, complex, float, float complex) that are affected.

John W. Eaton <jwe>
Wed 20 Jul 2016 10:14:24 AM UTC, comment #3:

Thanks John.

As the Matlab docs are unclear about right division by an array of arbitrary dimensions, I experimented a bit more.
It turns out that mrdivide, rdivide and "/", and for that matter, mldivide too, all perform element-by-element division for anything except square matrices. Obvious as that's the only useful outcome for arbitrary arrays.
Matlab r2016b prerelease:
+verbatim-

>> format long
>> format compact
>> MX = rand (3, 3, 3)

MX(:,:,1) =
0.327565434075205   0.833500595588975   0.861980478702072
0.671264370451740   0.768854252429615   0.989872153631504
0.438644982586956   0.167253545494722   0.514423456505704
MX(:,:,2) =
0.884281023126955   0.199862822857452   0.825583815786156
0.588026055308498   0.406954837138907   0.789963029944531
0.154752348656045   0.748705718215691   0.318524245398992
MX(:,:,3) =
0.534064127370726   0.136292548938299   0.189710406017580
0.089950678770581   0.678652304800188   0.495005824990221
0.111705744193203   0.495177019089661   0.147608221976689

>> (1 / MX) - mrdivide (1, MX)

ans(:,:,1) =
0     0     0
0     0     0
0     0     0
ans(:,:,2) =
0     0     0
0     0     0
0     0     0
ans(:,:,3) =
0     0     0
0     0     0
0     0     0

>> (1 / MX) - rdivide (1, MX)

ans(:,:,1) =
0     0     0
0     0     0
0     0     0
ans(:,:,2) =
0     0     0
0     0     0
0     0     0
ans(:,:,3) =
0     0     0
0     0     0
0     0     0

>> (1 / MX) - mldivide (MX, 1)

ans(:,:,1) =
0     0     0
0     0     0
0     0     0
ans(:,:,2) =
0     0     0
0     0     0
0     0     0
ans(:,:,3) =
0     0     0
0     0     0
0     0     0
-verbatim-

For square matrix:

>> SMX = rand (2, 2)
SMX =
0.054974146906188   0.560559527354885
0.850712674289007   0.929608866756663
>> 1 / SMX
Error using  /
Matrix dimensions must agree.
>> 1 ./ SMX
ans =
18.190368678325679   1.783931859509561
1.175485014180330   1.075721236920778
>> rdivide (1, SMX)
ans =
18.190368678325679   1.783931859509561
1.175485014180330   1.075721236920778
>> mrdivide (1, SMX)
Error using  /
Matrix dimensions must agree.
>> mldivide (SMX, 1)
Error using  \
Matrix dimensions must agree.

Octave-4.1.0+ does:
+verbatim-

>> 1 / MX

error: operator /: nonconformant arguments (op1 is 1x1, op2 is 3x9)

>> mrdivide (1, MX)

error: mrdivide: operator /: nonconformant arguments (op1 is 1x1, op2 is 3x9)

>> rdivide (1, MX)

ans =
ans(:,:,1) =
3.05282516399582   1.19975919068585   1.16011908008143
1.48972602154801   1.30063662500396   1.01023146911583
2.27974795038665   5.97894649731989   1.94392379926188
ans(:,:,2) =
1.13086221896275   5.00343178237420   1.21126405445310
1.70060491533044   2.45727512917771   1.26588202497301
6.46193746773186   1.33563825635417   3.13947843671169
ans(:,:,3) =
1.87243431780963    7.33715825105533    5.27119213432779
11.11720349048724    1.47350858890021    2.02017824743730
8.95209111422619    2.01947982529240    6.77469037028254

>> mldivide (MX, 1)

error: mldivide: operator \: nonconformant arguments (op1 is 3x9, op2 is 1x1)>>
-verbatim-

Is "Matlab compatibility" the proper Item Group or should it rather be one of "Feature request", "WTF, Matlab?!?" or "Incorrect result"?

And do you want a separate bug report for the ND to 2x2 array conversion?

Philip Nienhuis <philipnienhuis>
Wed 20 Jul 2016 12:08:26 AM UTC, comment #2:

mrdivide is the functional equivalent of the "/" operator.

rdivide is the same as "./", the element-by-element operator.

However, it does look like a bug for Octave to be trying convert an N-d array to a 2-d matrix in this case.

John W. Eaton <jwe>
Tue 19 Jul 2016 09:49:22 PM UTC, comment #1:

Checking a bit more it looks like Matlab interprets "1 / <ND_array>" as "rdivide (1, <matrix>)".

That is at least a workaround.

Why Matlab doesn't accept "1 / <2x2 matrix>" is beyond me, as "rdivide (1, <2x2 matrix>)" goes fine. Maybe because 1/<square matrix> does have a meaning different from element-wise division?

Philip Nienhuis <philipnienhuis>
Tue 19 Jul 2016 09:03:29 PM UTC, original submission:

Matlab r2016b prerelease accepts "1 / rand (5, 5, 5);"
I'm unsure about how to interpret this but it seems to work fine in Matlab.

In Octave:

>> 1 / rand (5, 5, 5)
error: flow: operator /: nonconformant arguments (op1 is 1x1, op2 is 5x25)

I don't know if this qualifies as a bug or just a Matlab feature not (yet?) implemented in Octave; provisionally I assumed the latter.
Octave's error message suggests the second & third dimension of "rand (5, 5, 5)" aren't recognized properly; does that suggest a different bug?

To be sure, Matlab in turn doesn't accept a simple 2D matrix at the RHS:

>> 1 / rand (2, 2)
Error using /
Matrix dimensions must agree

At least Octave and Matlab agree there :-)

I hit this when trying out reducepatch.m (patch #8912) with the examples from Matlab itself.

(Mageia5 64b, hg id ef91e43f162a "set DLL ....")

Philip Nienhuis <philipnienhuis>

## Attached Files

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

Attach Files:

Comment:

Attached Files
file #37954:  diffs.txt added by jwe (4KiB - text/plain)

## Dependencies

Depends on the following items: None found

Items that depend on this one: None found

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

Do you think this task is very important?
This task has 0 encouragements so far.

Only project members can vote.