From 3f1d0bc5e0d83147f7485426adfddd2ae3a15fd5 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Wed, 16 Nov 2016 11:15:37 +0100 Subject: [PATCH] Generate: add linear range for integers - LinearRangeInt32 --- src/Numerics/Generate.cs | 50 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Numerics/Generate.cs b/src/Numerics/Generate.cs index ad0d09d0..63281dd0 100644 --- a/src/Numerics/Generate.cs +++ b/src/Numerics/Generate.cs @@ -3,7 +3,7 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // -// Copyright (c) 2009-2014 Math.NET +// Copyright (c) 2009-2016 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -215,6 +215,33 @@ namespace MathNet.Numerics } } + /// + /// Generate a linearly spaced sample vector within the inclusive interval (start, stop) and step 1. + /// Equivalent to MATLAB colon operator (:). + /// + public static int[] LinearRangeInt32(int start, int stop) + { + if (start == stop) return new int[] { start }; + if (start < stop) + { + var data = new int[stop - start + 1]; + for (int i = 0; i < data.Length; i++) + { + data[i] = start + i; + } + return data; + } + else + { + var data = new int[start - stop + 1]; + for (int i = 0; i < data.Length; i++) + { + data[i] = start - i; + } + return data; + } + } + /// /// Generate a linearly spaced sample vector within the inclusive interval (start, stop) and the provided step. /// The start value is aways included as first value, but stop is only included if it stop-start is a multiple of step. @@ -236,6 +263,27 @@ namespace MathNet.Numerics return data; } + /// + /// Generate a linearly spaced sample vector within the inclusive interval (start, stop) and the provided step. + /// The start value is aways included as first value, but stop is only included if it stop-start is a multiple of step. + /// Equivalent to MATLAB double colon operator (::). + /// + public static int[] LinearRangeInt32(int start, int step, int stop) + { + if (start == stop) return new int[] { start }; + if (start < stop && step < 0 || start > stop && step > 0 || step == 0d) + { + return new int[0]; + } + + var data = new int[(stop - start) / step + 1]; + for (int i = 0; i < data.Length; i++) + { + data[i] = start + i * step; + } + return data; + } + /// /// Generate a linearly spaced sample vector within the inclusive interval (start, stop) and the provide step. /// The start value is aways included as first value, but stop is only included if it stop-start is a multiple of step.