diff liboctave/fMatrix.cc @ 9653:e087d7c77ff9

improve linspace in liboctave
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 18 Sep 2009 15:27:09 +0200
parents a9b37bae1802
children 3429c956de6f
line wrap: on
line diff
--- a/liboctave/fMatrix.cc
+++ b/liboctave/fMatrix.cc
@@ -3394,6 +3394,39 @@
   return result;
 }
 
+FloatMatrix linspace (const FloatColumnVector& x1, 
+                      const FloatColumnVector& x2, 
+                      octave_idx_type n)
+
+{
+  if (n < 1) n = 1;
+
+  octave_idx_type m = x1.length ();
+
+  if (x2.length () != m)
+    (*current_liboctave_error_handler) ("linspace: vectors must be of equal length");
+
+  NoAlias<FloatMatrix> retval;
+
+  retval.clear (m, n);
+  for (octave_idx_type i = 0; i < m; i++)
+    retval(i, 0) = x1(i);
+
+  // The last column is not needed while using delta.
+  float *delta = &retval(0, 1); 
+  for (octave_idx_type i = 0; i < m; i++)
+    delta[i] = (x2(i) - x1(i)) / (n - 1);
+
+  for (octave_idx_type j = 1; j < n-1; j++)
+    for (octave_idx_type i = 0; i < m; i++)
+      retval(i, j) = retval(i, j-1) + delta[i];
+
+  for (octave_idx_type i = 0; i < m; i++)
+    retval(i, n-1) = x2(i);
+
+  return retval;
+}
+
 MS_CMP_OPS (FloatMatrix, float)
 MS_BOOL_OPS (FloatMatrix, float)