]> gitweb.michael.orlitzky.com - octave.git/blobdiff - optimization/test_functions/extended_rosenbrock_gradient1.m
Add extended_rosenbrock_gradient1() and its tests.
[octave.git] / optimization / test_functions / extended_rosenbrock_gradient1.m
diff --git a/optimization/test_functions/extended_rosenbrock_gradient1.m b/optimization/test_functions/extended_rosenbrock_gradient1.m
new file mode 100644 (file)
index 0000000..2fd3c8c
--- /dev/null
@@ -0,0 +1,31 @@
+function g = extended_rosenbrock_gradient1(x)
+  ##
+  ## The gradient of the extended Rosenbrock function. See
+  ## extended_rosenbrock1.m for more information.
+  ##
+  ## Since the number of arguments is variable, we take a vector
+  ## instead of its individual components.
+  ##
+  n = length(x);
+
+  if (odd(n))
+    ## 'm' below must be an integer.
+    g = NA;
+    return;
+  end
+
+  m = n / 2;
+  g = zeros(n, 1);
+
+  % The extended Rosenbrock is simply a sum of Rosenbrock
+  % applications.
+  for k = [ 1 : m ]
+    y1 = x(2*k - 1);
+    y2 = x(2*k);
+
+    g_k = rosenbrock_gradient(y1, y2);
+
+    g(2*k - 1) = g(2*k - 1) + g_k(1);
+    g(2*k) = g(2*k) + g_k(2);
+  end
+end