[sage-support] Re: expression evaluation with dynamically generated variables
On Apr 24, 2009, at 11:55 PM, Eric wrote: Hello, I need to be able to evaluate an expression, where the expression is composed of dynamically generated variables. For example, I might have a list var_list, a list data_list, and an expression exprssn. Here 1) var_list is a list of variables, where every element in var_list was created using sage.calculus.calculus.var 2) data_list is a list of real numbers 3) exprssn is an expression, where every variable comes from var_list How would I evaluate exprssn when the variable var_list[i] takes the value data_list[i]? If I try something like exprssn(var_list[i] = data_list[i]) I get an error. My current solution involves string generation coupled with eval statements and is horribly clunky. What is the right way to do this? How about this: sage: var_list = var('x,y,z') sage: data_list = [2,3,5] sage: f = x^3 - y*z sage: f.subs(dict(zip(var_list, data_list))) -7 The zip command takes two lists, and zips them together. The dict can take a list of 2-tuples as a constructor. sage: zip(var_list, data_list) [(x, 2), (y, 3), (z, 5)] sage: dict(zip(var_list, data_list)) {y: 3, x: 2, z: 5} - Robert --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: expression evaluation with dynamically generated variables
Thanks, I think that answered my question! On Apr 25, 12:35 am, Robert Bradshaw rober...@math.washington.edu wrote: On Apr 24, 2009, at 11:55 PM, Eric wrote: Hello, I need to be able to evaluate an expression, where the expression is composed of dynamically generated variables. For example, I might have a list var_list, a list data_list, and an expression exprssn. Here 1) var_list is a list of variables, where every element in var_list was created using sage.calculus.calculus.var 2) data_list is a list of real numbers 3) exprssn is an expression, where every variable comes from var_list How would I evaluate exprssn when the variable var_list[i] takes the value data_list[i]? If I try something like exprssn(var_list[i] = data_list[i]) I get an error. My current solution involves string generation coupled with eval statements and is horribly clunky. What is the right way to do this? How about this: sage: var_list = var('x,y,z') sage: data_list = [2,3,5] sage: f = x^3 - y*z sage: f.subs(dict(zip(var_list, data_list))) -7 The zip command takes two lists, and zips them together. The dict can take a list of 2-tuples as a constructor. sage: zip(var_list, data_list) [(x, 2), (y, 3), (z, 5)] sage: dict(zip(var_list, data_list)) {y: 3, x: 2, z: 5} - Robert --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---