bugGNU Octave - Bugs: bug #64861, qr with single return value.


bug #64861: qr with single return value.

Submitter:  Liang Tang <lt1234>
Submitted:  Sun 05 Nov 2023 07:52:57 PM UTC
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  lt1234 Open/Closed:  * Open
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  10.1.0 (current default)
* Mandatory Fields

Add a New Comment Rich Markup

Fri 12 Jan 2024 05:47:40 AM UTC, comment #4: 

It has always been confusing that the output depended on the sparsity of the input.  I think we should follow Matlab and guarantee that the one-output form always produces the matrix R.  I marked the Planned Release as 10.1.

There is an easy fix, which is just to call triu() on the output X.  The better fix, however, would be to change the code in liboctave/numeric/qr.cc so that it returns the correct value from the raw Fortran results.

Rik <rik5>
Group administrator
Tue 07 Nov 2023 05:11:44 PM UTC, comment #3: 

Doc for qr says

If just a single return value is requested then it is either @var{R}, if @var{A} is sparse, or @var{X}, such that @code{@var{R} = triu (@var{X})} if @var{A} is full.  (Note: unlike most commands, the single return value is not the first return value when multiple values are requested.)

It seems like a conscious design choice, not an oversight, so it must have been done to match Matlab back when it was written. If it's not compatible any longer, it can be changed to match Matlab's current behavior.

Arun Giridhar <arungiridhar>
Group Member
Tue 07 Nov 2023 03:54:52 PM UTC, comment #2: 

Nick, thanks for looking into this.  I do have a side request.  This is the first time I encountered that the first documented output is not returned when a single return value is requested.  For qr, limited by what I know, I can use q w/o knowing what r is but I cannot use r w/o q.  Therefore I was very surprised by the fact that r is returned instead of q.  In case anyone knows why r is returned (other than matlab compatibility), I would appreciate very much for the added information.  Thanks.

Liang Tang <lt1234>
Tue 07 Nov 2023 03:26:09 PM UTC, comment #1: 

just confirming this incompatibility exists with Octave 9 (hg id: 9c955ab01f0) and Matlab 2023b:

Matlab 2023b

>> A = [1 2 3]
A =
     1     2     3
>> [q r] = qr(A')
q =
   -0.2673   -0.5345   -0.8018
   -0.5345    0.7745   -0.3382
   -0.8018   -0.3382    0.4927
r =
>> a = qr(A')
a =
>> as = qr(sparse(A')), full(as)
as =
   (1,1)      -3.7417
ans =

Octave 9:

octave:1> A = [1 2 3]
A =

   1   2   3

octave:2> [q r] = qr(A')
q =

  -0.2673  -0.5345  -0.8018
  -0.5345   0.7745  -0.3382
  -0.8018  -0.3382   0.4927

r =


octave:3> a = qr(A')
a =


octave:4> as = qr(sparse(A')), full(as)
as =

Compressed Column Sparse (rows = 3, cols = 1, nnz = 1 [33%])

  (1, 1) -> -3.7417

ans =


It appears this was a change in Matlab R2022a. From the current public documentation for qr:
"R2022a: One-output syntax always returns upper-triangular factor
The syntax R = qr(A) always returns R as an upper-triangular matrix, regardless of whether A is full or sparse. Previously, for full A, the one-output syntax returned an R matrix with Householder vectors located in the lower-triangular portion of the matrix. These vectors partially defined the Q matrix."

we could consider simplifying the function to be compatible.  it's a rare case to see a function produce a different output for the same value input depending on whether the input is sparse or full.

Nicholas Jankowski <nrjank>
Group Member
Sun 05 Nov 2023 07:52:57 PM UTC, original submission:  

qr with a single return value is not compatible with matlab.  Thanks. 


R = qr(A) returns the upper-triangular R factor of the QR decomposition A = Q*R.


If just a single return value is requested then it is either R, if A is sparse, or X, such that 'R = triu (X)' if A is full.

I ran a simple example.  Two software follow their own documentation. 

A=[1 2 3];
[q r]=qr(A')
as=qr(sparse(A')), full(as)

Liang Tang <lt1234>


(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 rik5 (Posted a comment)
  • -email is unavailable- added by arungiridhar (Posted a comment)
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by lt1234 (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 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2024-01-12 rik5 Planned ReleaseNone 10.1.0 (current default)
    2023-11-07 nrjank StatusNone Confirmed
        Release8.1.0 dev
        Operating SystemMicrosoft Windows Any

    Back to the top

    Powered by Savane 3.13-599c.
    Corresponding source code