/* Original version of this file copyright 1999 by Michael Wester,
 * and retrieved from http://www.math.unm.edu/~wester/demos/Calculus/problems.macsyma
 * circa 2006-10-23.
 *
 * Released under the terms of the GNU General Public License, version 2,
 * per message dated 2007-06-03 from Michael Wester to Robert Dodier
 * (contained in the file wester-gpl-permission-message.txt).
 *
 * See: "A Critique of the Mathematical Abilities of CA Systems"
 * by Michael Wester, pp 25--60 in
 * "Computer Algebra Systems: A Practical Guide", edited by Michael J. Wester
 * and published by John Wiley and Sons, Chichester, United Kingdom, 1999.
 */
/* ----------[ M a c s y m a ]---------- */
/* ---------- Initialization ---------- */
showtime: all$
prederror: false$
/* ---------- Calculus ---------- */
/* Calculus on a non-smooth (but well defined) function => x/|x| or sign(x)
   */
diff(abs(x), x);
/* Calculus on a piecewise defined function */
a(x):= if x < 0 then -x else x$
/* => if x < 0 then -1 else 1 */
diff(a(x), x);
a(x):= -x*unit_step(-x) + x*unit_step(x)$
ratsimp(diff(a(x), x));
remfunction(a)$
/* Derivative of a piecewise defined function at a point [Herbert Fischer].
   f(x) = x^2 - 1 for x = 1 otherwise x^3.  f(1) = 0 and f'(1) = 3 */
f(x):= if x = 1 then x^2 - 1 else x^3$
f(1);
diff(f(x), x);
subst(x = 1, %);
remfunction(f)$
/* d^n/dx^n(x^n) => n! */
declare(n, integer)$
diff(x^n, x, n);
makefact(%);
remove(n, integer)$
/* Apply the chain rule---this is important for PDEs and many other
   applications => y_xx (x_t)^2 + y_x x_tt */
derivabbrev: true$
/* Set up implicit functional dependencies */
depends(y, x, x, t);
diff(y, t, 2);
remove([y, x], dependency)$
derivabbrev: false$
/* => f(h(x)) dh/dx - f(g(x)) dg/dx */
'integrate(f(y), y, g(x), h(x));
diff(%, x);
/* Exact differential => d(V(P, T)) => dV/dP DP + dV/dT DT */
diff(V(P, T));
/* Implicit differentiation => dy/dx = [1 - y sin(x y)] / [1 + x sin(x y)] */
y = cos(x*y) + x;
solve(diff(%), del(y))[1] / del(x);
/* => 2 (x + y) g'(x^2 + y^2) */
eqn: 'diff(f(x, y), x) + 'diff(f(x, y), y);
ev(subst(f(x, y) = g(x^2 + y^2), eqn), diff);
load(ndiff)$
ev(subst(f(x, y) = g(x^2 + y^2), eqn), diff);
subst(f(x, y) = g(x^2 + y^2), eqn);
factor(ev(%, diff));
convert_to_de(%);
remvalue(eqn)$
/* Residue => - 9/4 */
residue((z^3 + 5)/((z^4 - 1)*(z + 1)), z, -1);
/* Differential forms */
init_cartan([x, y, z])$
/* (2 dx + dz) /\ (3 dx + dy + dz) /\ (dx + dy + 4 dz) => 8 dx /\ dy /\ dz */
(2*dx + dz) ~ (3*dx + dy + dz) ~ (dx + dy + 4*dz);
/* d(3 x^5 dy /\ dz + 5 x y^2 dz /\ dx + 8 z dx /\ dy)
   => (15 x^4 + 10 x y + 8) dx /\ dy /\ dz */
factor(ext_diff(3*x^5 * dy ~ dz + 5*x*y^2 * dz ~ dx + 8*z * dx ~ dy));
/* => 1 - 3/8 2^(1/3) = 0.5275296 */
f(x):= x^4 - x + 1$
min_bracket(0.0, 1.0, f);
errcatch(apply('min_function, endcons(f, %[1])));
/* => [0, 1] */
/*[minimize(1/(x^2 + y^2 + 1)), maximize(1/(x^2 + y^2 + 1))];*/
/* Minimize on [-1, 1] x [-1, 1]:
   => min(a - b - c + d, a - b + c - d, a + b - c - d, a + b + c + d) */
/*minimize(a + b*x + c*y + d*x*y, [x, -1, 1], [y, -1, 1]);*/
/* => [-1, 1] */
/*[minimize(x^2*y^3, [x, -1, 1], [y, -1, 1]),
   maximize(x^2*y^3, [x, -1, 1], [y, -1, 1])];*/
/* Linear programming: minimize the objective function z subject to the
   variables xi being non-negative along with an additional set of constraints.
   See William R. Smythe, Jr. and Lynwood A. Johnson, _Introduction to Linear
   Programming, with Applications_, Prentice Hall, Inc., 1966, p. 117:
   minimize z = 4 x1 - x2 + 2 x3 - 2 x4 => {x1, x2, x3, x4}  = {2, 0, 2, 4}
   with zmin = 4 */
lp_by_simplex(-(4*x1 - x2 + 2*x3 - 2*x4), [2*x1 + x2 + x3 + x4 <= 10,
              x1 - 2*x2 - x3 + x4 >= 4, x1 + x2 + 3*x3 - x4 >= 4],
              [x1, x2, x3, x4]);
