[Pkg-octave-commit] [SCM] octave-symbolic branch, master, updated. a718b53403d9f164f8c2a3df521385a01d28a11b

watsma watsma at 416fae20-06d0-4450-9b69-c6c34d4b5f03
Mon Jan 3 03:40:35 UTC 2011


The following commit has been merged in the master branch:
commit c05706264b31e9e7ad42e9008f0677a4aff92998
Author: watsma <watsma at 416fae20-06d0-4450-9b69-c6c34d4b5f03>
Date:   Mon May 5 21:36:45 2003 +0000

    Added get_relation()
    Fixed a remaining multiline-string - added \n\
    Modified subs() to also accept list or cell arguments for old and new
      expressions. Updated the documentation string.
    
    
    git-svn-id: https://octave.svn.sourceforge.net/svnroot/octave/trunk/octave-forge/main/symbolic@921 416fae20-06d0-4450-9b69-c6c34d4b5f03

diff --git a/symbols.cc b/symbols.cc
index 2b9217a..66f2fd0 100644
--- a/symbols.cc
+++ b/symbols.cc
@@ -21,6 +21,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 // 2001-09-18 Paul Kienzle <pkienzle at users.sf.net>
 // * use GiNaC::is_a<GiNaC::blah>(x) rather than is_ex_of_type(x, blah)
 // * use GiNaC::ex_to<GiNaC::blah>(x) rather than ex_to_blah(x)
+// 2003-04-19 Willem Atsma <watsma at users.sf.net>
+// * added get_relational()
 
 #include <octave/config.h>
 #include <octave/defun-dld.h>
@@ -104,6 +106,17 @@ bool get_numeric(const octave_value arg, GiNaC::numeric& number)
   return true;
 }
 
+bool get_relation(const octave_value arg, GiNaC::relational& relation)
+{
+	const octave_value& rep = arg.get_rep();
+	if (arg.type_id () == octave_relational::static_type_id ()) {
+		GiNaC::relational x = ((octave_relational& ) rep).relational_value();
+		relation = x;
+	} else return false;
+	
+	return true;
+}
+
 DEFUN_DLD(symbols,args,,"Initialize symbolic manipulation")
 {
   octave_value retval;
@@ -249,62 +262,86 @@ DEFUN_DLD(subs,args,,
 "-*- texinfo -*-\n\
 @deftypefn Loadable Function {b =} subs(@var{a}, at var{x}, at var{n})\n\
 \n\
-Substitute a number for a variables in an expression\n\
+Substitute variables in an expression.\n\
 @table @var\n\
 @item a\n\
  The expresion in which the substition will occur.\n\
 @item x\n\
- The variable that will be substituted.\n\
+ The variables that will be substituted.\n\
 @item n\n\
- The expression,vpa value or scalar that will replace x.\n\
+ The expressions, vpa values or scalars that will replace @var{x}.\n\
 @end table\n\
 \n\
+Multiple substitutions can be made by using lists or cell-arrays for\n\
+ at var{x} and @var{n}.\n\
+\n\
+Examples:\n\
+ at example\n\
+symbols\n\
+x=sym(\"x\"); y=sym(\"y\"); f=x^2+3*x*y-y^2;\n\
+v = subs (f,x,1)\n\
+w = subs (f,@{x,y@},@{1,vpa(1/3)@})\n\
+ at end example\n\
+\n\
 @end deftypefn\n\
 ")
 {
-  GiNaC::ex expression;
-  GiNaC::ex the_sym;
-  GiNaC::ex ex_sub;
-  GiNaC::ex tmp;
-  int nargin = args.length ();
-  octave_value retval;
-
-  if (nargin != 3)
-    {
-      error("need three arguments\n");
-      return retval;
-    }
-
-  try 
-    {
-      if (!get_expression (args(0), expression))
-	{
-	  gripe_wrong_type_arg ("subs",args(0));
-	  return retval;
-	}
-
-      if (!get_symbol (args(1), the_sym))
-	{
-	  gripe_wrong_type_arg("subs",args(1));
-	  return retval;
+	GiNaC::ex expression;
+	GiNaC::ex the_sym;
+	GiNaC::ex ex_sub;
+	GiNaC::ex tmp;
+	int nargin = args.length ();
+	octave_value retval;
+
+	if (nargin != 3) {
+		error("need three arguments\n");
+		return retval;
 	}
 
-      if (!get_expression (args(2), ex_sub))
-	{
-	  gripe_wrong_type_arg ("subs",args(2));
-	  return retval;
+	try {
+		if (!get_expression (args(0), expression)) {
+			gripe_wrong_type_arg ("subs",args(0));
+			return retval;
+		}
+		if (!(args(1).is_list() || args(1).is_cell())) {
+			if (!get_symbol (args(1), the_sym)) {
+				gripe_wrong_type_arg("subs",args(1));
+				return retval;
+			}
+			if (!get_expression (args(2), ex_sub)) {
+				gripe_wrong_type_arg ("subs",args(2));
+				return retval;
+			}
+			tmp = expression.subs(the_sym == ex_sub);
+		} else {
+			octave_value_list symlist, sublist;
+			int i;
+			symlist = args(1).list_value();
+			sublist = args(2).list_value();
+			if(symlist.length()!=sublist.length()) {
+				error("Number of expressions and substitutes must be the same.");
+				return retval;
+			}
+			tmp = expression;
+			for(i=0;i<symlist.length();i++) {
+				if (!get_symbol (symlist(i),the_sym)) {
+					gripe_wrong_type_arg("subs",symlist(i));
+					return retval;
+				}
+				if (!get_expression (sublist(i),ex_sub)) {
+					gripe_wrong_type_arg("subs",sublist(i));
+					return retval;
+				}
+				tmp = tmp.subs(the_sym == ex_sub);
+			}
+		}
+		retval = octave_value (new octave_ex(tmp));
+	} catch (std::exception &e) {
+		error (e.what ());
+		retval = octave_value ();
 	}
 
-      tmp = expression.subs(the_sym == ex_sub);
-      retval = octave_value (new octave_ex(tmp));
-    }
-  catch (std::exception &e)
-    {
-      error (e.what ());
-      retval = octave_value ();
-    }
-
-  return retval;
+	return retval;
 }
 
 DEFUN_DLD(expand,args,,
@@ -361,7 +398,7 @@ DEFUN_DLD(coeff,args,,
 "-*- texinfo -*-\n\
 @deftypefn {Loadable Function} {b =} coeff(@var{a}, at var{x}, at var{n})\n\
 \n\
-Obtain the @var{n}th coefficient of the variable @var{x} in @var{a}.  
+Obtain the @var{n}th coefficient of the variable @var{x} in @var{a}.\n\
 \n\
 @end deftypefn\n\
 ")
diff --git a/symbols.h b/symbols.h
index a0a9d2b..43af378 100644
--- a/symbols.h
+++ b/symbols.h
@@ -1,6 +1,7 @@
 bool get_expression(const octave_value arg, GiNaC::ex& expression);
 bool get_symbol(const octave_value arg, GiNaC::ex& sym);
 bool get_numeric(const octave_value arg, GiNaC::numeric& number);
+bool get_relation(const octave_value arg, GiNaC::relational& rel);
 
 #define DEFUN_DLD_EX_GINAC_FUNCTION(oct_name,ginac_name,description) \
 DEFUN_DLD(oct_name, args, , \

-- 
octave-symbolic



More information about the Pkg-octave-commit mailing list