Package jisa.maths

Class Range<T extends java.lang.Number>

  • Type Parameters:
    T - Type of number (ie Double or Integer).
    All Implemented Interfaces:
    java.lang.Iterable<T>
    Direct Known Subclasses:
    Range.DoubleRange

    public class Range<T extends java.lang.Number>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    Class for defining ranges of numbers to iterate over.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Range.DoubleRange  
      static class  Range.Type  
    • Constructor Summary

      Constructors 
      Constructor Description
      Range​(T[] data)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T[] array()
      Returns this range as an array of values.
      RealMatrix column()
      Reshapes this range into a column matrix.
      static Range<java.lang.Integer> count​(int start, int stop)
      Creates a range of integer numbers, represented as integers.
      double[] doubleArray()
      Returns this range as an array of double values.
      static Range<java.lang.Double> exponential​(java.lang.Number start, java.lang.Number stop, int noSteps)
      Creates a geometric series of values from "start" and ending at "stop" with the geometric factor determined by a fixed number of steps.
      static Range<java.lang.Double> function​(int start, int stop, GFunction<java.lang.Double,​java.lang.Double> function)
      Creates a range of numbers defined by a custom series function.
      static Range<java.lang.Double> geometric​(java.lang.Number start, java.lang.Number stop, java.lang.Number factor)
      Creates a geometric series of numbers starting at "start", ending before exceeding "stop" with a defined geometric/multiplicative factor.
      T get​(int index)
      Returns the nth element in this range.
      double getDouble​(int index)
      Returns the nth element in this range as a double.
      java.util.Iterator<T> iterator()  
      static Range<java.lang.Double> linear​(int start, int stop)
      Creates a range of integer numbers, represented as doubles.
      static Range<java.lang.Double> linear​(java.lang.Number start, java.lang.Number stop, int numSteps)
      Creates a range of equally-spaced numbers with a defined number of steps.
      java.util.List<T> list()
      Returns this range as a list of values.
      static Range<java.lang.Double> manual​(java.lang.Number... values)  
      Range<T> mirror()
      Returns a copy of this range, but with its reverse appended on the end.
      static Range<java.lang.Double> polynomial​(java.lang.Number start, java.lang.Number stop, int noSteps, int order)  
      Range<T> repeat​(int times)
      Returns a range containing this range plus itself again n times.
      static Range<java.lang.Double> repeat​(java.lang.Number value, int numTimes)
      Creates a range of the same number repeated n times.
      RealMatrix reshape​(int rows, int cols)
      Reshapes this range into an NxM matrix (where N*M must equal size()).
      Range<T> reverse()
      Returns a copy of this range but with its elements in reverse-order.
      RealMatrix row()
      Reshapes this range into a row matrix.
      Range<T> shift​(int places)
      Returns a copy of this range but with all elements cyclically shifted by n places (+ve to the right, -ve to the left).
      Range<T> shuffle()
      Returns a copy of this range but with its elements in a random order.
      int size()
      Returns the number of elements in this range
      static Range<java.lang.Double> step​(java.lang.Number start, java.lang.Number stop, java.lang.Number step)
      Creates an arithmetic series, starting at "start", stopping before exceeding "stop" with a defined step size.
      java.util.stream.Stream<T> stream()  
      java.lang.String toString()
      Returns a string representation of this range.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • Range

        public Range​(T[] data)
    • Method Detail

      • manual

        public static Range<java.lang.Double> manual​(java.lang.Number... values)
      • linear

        public static Range<java.lang.Double> linear​(java.lang.Number start,
                                                     java.lang.Number stop,
                                                     int numSteps)
        Creates a range of equally-spaced numbers with a defined number of steps.
        Parameters:
        start - The number to start at
        stop - The number to end at
        numSteps - The total number of values to generate
        Returns:
        Linear range
      • linear

        public static Range<java.lang.Double> linear​(int start,
                                                     int stop)
        Creates a range of integer numbers, represented as doubles.
        Parameters:
        start - Integer to start at
        stop - Integer to end at
        Returns:
        Integer range
      • count

        public static Range<java.lang.Integer> count​(int start,
                                                     int stop)
        Creates a range of integer numbers, represented as integers.
        Parameters:
        start - Integer to start at
        stop - Integer to end at
        Returns:
        Integer range
      • exponential

        public static Range<java.lang.Double> exponential​(java.lang.Number start,
                                                          java.lang.Number stop,
                                                          int noSteps)
        Creates a geometric series of values from "start" and ending at "stop" with the geometric factor determined by a fixed number of steps.
        Parameters:
        start - Number to start at
        stop - Number to end at
        noSteps - Total number of elements
        Returns:
      • geometric

        public static Range<java.lang.Double> geometric​(java.lang.Number start,
                                                        java.lang.Number stop,
                                                        java.lang.Number factor)
        Creates a geometric series of numbers starting at "start", ending before exceeding "stop" with a defined geometric/multiplicative factor.
        Parameters:
        start - Number to start at
        stop - Number to stop at or before
        factor - Multiplicative or geometric factor
        Returns:
        Geometric range
      • polynomial

        public static Range<java.lang.Double> polynomial​(java.lang.Number start,
                                                         java.lang.Number stop,
                                                         int noSteps,
                                                         int order)
      • step

        public static Range<java.lang.Double> step​(java.lang.Number start,
                                                   java.lang.Number stop,
                                                   java.lang.Number step)
        Creates an arithmetic series, starting at "start", stopping before exceeding "stop" with a defined step size.
        Parameters:
        start - Number to start at
        stop - Number to end at, or before
        step - Step size
        Returns:
        Arithmetic range
      • repeat

        public static Range<java.lang.Double> repeat​(java.lang.Number value,
                                                     int numTimes)
        Creates a range of the same number repeated n times.
        Parameters:
        value - Value to repeat
        numTimes - Number of elements
        Returns:
        Repeated range
      • function

        public static Range<java.lang.Double> function​(int start,
                                                       int stop,
                                                       GFunction<java.lang.Double,​java.lang.Double> function)
        Creates a range of numbers defined by a custom series function.
        Parameters:
        start - Index to start at
        stop - Index to stop at
        function - Function to generate values
        Returns:
        Custom range
      • reverse

        public Range<T> reverse()
        Returns a copy of this range but with its elements in reverse-order.

        Example: [4, 2, 7] becomes [7, 2, 4]

        Returns:
        Reversed range
      • mirror

        public Range<T> mirror()
        Returns a copy of this range, but with its reverse appended on the end.

        Example: [1, 2, 3] becomes [1, 2, 3, 3, 2, 1]

        Returns:
        Mirrored range
      • repeat

        public Range<T> repeat​(int times)
        Returns a range containing this range plus itself again n times.

        Example: [1, 2, 3] -> repeat(1) -> [1, 2, 3, 1, 2, 3]

        Parameters:
        times - Number of repeats to append
        Returns:
        Repeated range
      • shift

        public Range<T> shift​(int places)
        Returns a copy of this range but with all elements cyclically shifted by n places (+ve to the right, -ve to the left).

        Example: [1, 2, 3] -> shift(+2) -> [2, 3, 1] Example: [1, 2, 3] -> shift(-2) -> [3, 1, 2]

        Parameters:
        places - Places to shift by
        Returns:
        Shifted range
      • shuffle

        public Range<T> shuffle()
        Returns a copy of this range but with its elements in a random order.

        Example: [1, 2, 3, 4] may become [3, 1, 4, 2]

        Returns:
        Shuffled array
      • array

        public T[] array()
        Returns this range as an array of values.
        Returns:
        Array of values
      • list

        public java.util.List<T> list()
        Returns this range as a list of values.
        Returns:
        List of values
      • stream

        public java.util.stream.Stream<T> stream()
      • doubleArray

        public double[] doubleArray()
        Returns this range as an array of double values.
        Returns:
        Array of double values
      • size

        public int size()
        Returns the number of elements in this range
        Returns:
        Size of range
      • get

        public T get​(int index)
        Returns the nth element in this range.
        Parameters:
        index - Index of element to return
        Returns:
        Element at nth position
      • getDouble

        public double getDouble​(int index)
        Returns the nth element in this range as a double.
        Parameters:
        index - Index of element to return
        Returns:
        Element at nth position, as double
      • reshape

        public RealMatrix reshape​(int rows,
                                  int cols)
        Reshapes this range into an NxM matrix (where N*M must equal size()).
        Parameters:
        rows - Number of rows
        cols - Number of columns
        Returns:
        Resulting matrix.
      • column

        public RealMatrix column()
        Reshapes this range into a column matrix.
        Returns:
        Column matrix
      • row

        public RealMatrix row()
        Reshapes this range into a row matrix.
        Returns:
        Row matrix
      • toString

        public java.lang.String toString()
        Returns a string representation of this range.
        Overrides:
        toString in class java.lang.Object
        Returns:
        String representation
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T extends java.lang.Number>