bugGNU Octave - Bugs: bug #56659, Symmetry of linspace (-a, a, n)


bug #56659: Symmetry of linspace (-a, a, n)

Submitted by:  Marco Caliari <caliari>
Submitted on:  Tue 23 Jul 2019 11:25:16 AM UTC  
Category:  Octave Function Severity:  1 - Wish
Priority:  3 - Low Item Group:  Inaccurate Result
Status:  Patch Submitted Assigned to:  None
Originator Name:  Open/Closed:  Open
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


Thu 25 Jul 2019 06:20:08 PM UTC, comment #3: 

After a private communication with Michael, the modification needed would be something like in the attached linspace.diff (I modified only the double version).

(file #47260)

Marco Caliari <caliari>
Project Member
Tue 23 Jul 2019 12:36:51 PM UTC, comment #2: 

@Michael: I have used linspace hundreds (or thousands) of times, most of them in the symmetric case and I have never thought that the result was unsymmetric. My first example was trivial, try this one

x = linspace (-1000, 1000, 12);

I agree with you that there are several other special cases which we cannot treat. Maybe it would be enough to add an example in the documentation with a warning about the lost symmetry.

Marco Caliari <caliari>
Project Member
Tue 23 Jul 2019 12:19:28 PM UTC, comment #1: 

I was going to answer: because first this works only if BASE and LIMIT (in the sense of linspace's documentation) are symmetric with respect to zero, which will be fulfilled only in the minority of cases, and second because this converts a range unnecessarily to a full vector. But I have just seen that linspace does output a full vector! Is there a reason for that?

So the point is that linspace seems to construct a range by


and to return the conversion of this range to a full vector. And you see, this expression is not symmetric around zero. However, it is the most accurate expression for the case where BASE==0. Of course, you could code optimized expressions for all conceivable special cases into the internal functions. But I think it is more sensible to leave that to the user: if they really feel the need to shear off the last eps/2, then they shouldn't use a general purpose function like linspace(). For your specific example, just use


Michael Leitner <mleitner>
Tue 23 Jul 2019 11:25:16 AM UTC, original submission:  

If you generate a vector by

x = linspace (-1, 1, 11);

you discover that it is not symmetric

> x ==-fliplr (x)
ans =

  1  1  0  0  0  1  0  0  0  1  1

If you measure the difference you find

> max (abs (x + fliplr (x)))
ans =    2.2204e-16

(while it is eps/2 in matlab).
What about to correct by default the result by performing

x = (x - fliplr (x)) / 2;

at the end?

Marco Caliari <caliari>
Project Member


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

Attach Files:

Attached Files
file #47260:  linspace.diff added by caliari (788B - text/x-patch)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by mleitner (Posted a comment)
  • -email is unavailable- added by caliari (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.




    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-08-21 mtmiller Priority5 - Normal => 3 - Low
        StatusNone => Patch Submitted
    2019-07-25 caliari Attached File- => Added linspace.diff, #47260

    Back to the top

    Powered by Savane 3.5