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:  Fixed Assigned to:  None
Originator Name:  lt1234 Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  11.1.0 (current default) Planned Release:  10.1.0
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

Jump to the original submission

Thu 06 Nov 2025 10:01:06 AM UTC, comment #7: 

I don't know either.  It seems you really need both Q and R if you want to solve systems of linear equations.  In this case, Octave is just choosing to be Matlab-compatible so that any scripts written to rely on this particular choice of return values will still work.

Rik <rik5>
Group administrator
Mon 03 Nov 2025 01:49:49 PM UTC, comment #6: 

Thanks. 

FYI only.  I still cannot figure out why matlab puts out r, not q.  For me, q is more useful than r.

Liang Tang <lt1234>
Sun 02 Nov 2025 12:16:18 PM UTC, comment #5: 

This behavior of qr() was always questionable.  I changed Octave's qr function to return just the matrix R for dense matrix inputs, rather than the raw output from LAPACK which includes additional information in the lower-triangular part of the matrix.

See changeset https://hg.savann ... /rev/6b1293c0bbad.  This will be a part of the 11.1.0 release of Octave.

Marking as Fixed and closing report.

Rik <rik5>
Group administrator
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 =
   -3.7417
         0
         0
>> a = qr(A')
a =
   -3.7417
         0
         0
>> as = qr(sparse(A')), full(as)
as =
   (1,1)      -3.7417
ans =
   -3.7417
         0
         0


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 =

  -3.7417
        0
        0

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

  -3.7417
   0.5345
   0.8018

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

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

  (1, 1) -> -3.7417

ans =

  -3.7417
        0
        0


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. 


Matlab:

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

Octave

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')
a=qr(A')
as=qr(sparse(A')), full(as)


Liang Tang <lt1234>

 

Attached Files

This item currently has no attached files.

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

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

No items depend on this one.

 

Mail Notification Carbon-Copy List

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)
  •  

    Votes

    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.

     

    History

    Follow 7 latest changes.

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

    Back to the top

    Powered by Savane 3.16-ed84.
    Corresponding source code