diff liboctave/CMatrix.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/CMatrix.cc
+++ b/liboctave/CMatrix.cc
@@ -4066,6 +4066,39 @@
   return result;
 }
 
+ComplexMatrix linspace (const ComplexColumnVector& x1, 
+                        const ComplexColumnVector& 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<ComplexMatrix> 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.
+  Complex *delta = &retval(0, 1); 
+  for (octave_idx_type i = 0; i < m; i++)
+    delta[i] = (x2(i) - x1(i)) / (n - 1.0);
+
+  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 (ComplexMatrix, Complex)
 MS_BOOL_OPS (ComplexMatrix, Complex)