Re: [sage-support] How to create and use lists of variables?

2014-10-30 Thread smohammadh
Due to a system malfunction, I was not able to post my new question, 
immediately. So after a day, I've already posted it, titled as "Why is this 
Value Error raised?".

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-support] How to create and use lists of variables?

2014-10-29 Thread Michael Orlitzky
On 10/28/2014 10:47 PM, smohamm...@gmail.com wrote:
> Thank you so much! I'm sorry I couldn't answer earlier. It worked as I
> expected. Now as the last question on this discussion, do I need to use
> 'SymbolicSequence', when I want to numerically solve a large ODE system?
> I tried to use instructions in "Solving ODE numerically by GSL" from the
> Documentation, but I have encountered some unexpected 'Value Error'(s).
> Of course I know I should ask about them in a separate discussion (and
> I'll do it right after this post!), but here I want to know if it is
> beneficial (or even possible) to do such computations using
> 'SymbolicSequence'.

SymbolSequence is just syntactic sugar over the usual symbols you'd get
with e.g. SR.var(), so there shouldn't be any unnecessary limitations.

But I'm not familiar with what you're trying to do, so you're going to
have to let us know what that ValueError is that you're getting.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-support] How to create and use lists of variables?

2014-10-28 Thread smohammadh
Thank you so much! I'm sorry I couldn't answer earlier. It worked as I 
expected. Now as the last question on this discussion, do I need to use 
'SymbolicSequence', when I want to numerically solve a large ODE system? I 
tried to use instructions in "Solving ODE numerically by GSL" from the 
Documentation, but I have encountered some unexpected 'Value Error'(s). Of 
course I know I should ask about them in a separate discussion (and I'll do 
it right after this post!), but here I want to know if it is beneficial (or 
even possible) to do such computations using 'SymbolicSequence'.
Thanks for all of your answers and all who answered...

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-support] How to create and use lists of variables?

2014-10-25 Thread Michael Orlitzky
On 10/25/2014 11:40 PM, smohamm...@gmail.com wrote:
> Thank you Michael, I loaded the file and succeeded to make a list of
> symbols. But now and after evaluating my  expression in terms of its
> elements, how should I assign them different values I want the
> expression to be calculated for? (see a part of my code above, it may
> illustrate what I mean)
> Assigning X[1]=0, results to an attribute error:
> 
> AttributeError: SymbolSequence instance has no attribute '__setitem__'

You have to substitute the values into the expression -- just setting
the value of the variable to something else won't actually affect the
expression. I know that sounds confusing. Here's a simpler example of
what I mean without the list of symbols:

  sage: x = SR.var('x')
  sage: f = x^2 + 1

At this point, "f" is a symbolic expression, in terms of some other
symbolic expression "x". We can substitute certain values of "x" into
"f" to see how it would evaluate:

  sage: f(x = 3)
  10

But setting the value of "x" doesn't change what "f" is:

  sage: x = 3
  sage: f
  x^2 + 1

The sequences should work the same:

  sage: x = SymbolSequence('x')
  sage: f = x[1]^2 + x[2]
  sage: f
  x_1^2 + x_2
  sage: f(x_1 = 3)
  x_2 + 9
  sage: f(x_1 = 3, x_2 = 1)
  10

If you want to substitute a whole bunch of things at once
programmatically, you may need to use a dictionary. Suppose I want to
make a big matrix but I don't want to decide on the size or entries
beforehand (i.e. I need to do it at runtime).

  sage: d = 5
  sage: X = matrix(SR, d, d, x[0:d,0:d])
  sage: X
  [x_0_0 x_0_1 x_0_2 x_0_3 x_0_4]
  [x_1_0 x_1_1 x_1_2 x_1_3 x_1_4]
  [x_2_0 x_2_1 x_2_2 x_2_3 x_2_4]
  [x_3_0 x_3_1 x_3_2 x_3_3 x_3_4]
  [x_4_0 x_4_1 x_4_2 x_4_3 x_4_4]

Whe. Now I want to substitute for those values. I can't hard-code
X(x_0_0 = 0, x_0_1 = 1, ...) since I don't know how many there are
beforehand. But I can make a list of them!

  sage: substitutions=[ (x[i,j], i+j) for i in range(0,d)
  :   for j in range(0,d) ]
  sage: substitutions
  [(x_0_0, 0),
   (x_0_1, 1),
   (x_0_2, 2),
   (x_0_3, 3),
   (x_0_4, 4),
   (x_1_0, 1),
   (x_1_1, 2),
   (x_1_2, 3),
   (x_1_3, 4),
   (x_1_4, 5),
   (x_2_0, 2),
   (x_2_1, 3),
   (x_2_2, 4),
   (x_2_3, 5),
   (x_2_4, 6),
   (x_3_0, 3),
   (x_3_1, 4),
   (x_3_2, 5),
   (x_3_3, 6),
   (x_3_4, 7),
   (x_4_0, 4),
   (x_4_1, 5),
   (x_4_2, 6),
   (x_4_3, 7),
   (x_4_4, 8)]

Wh. Now all I need to do is turn that into a dictionary.
One more time:

  sage: subs_dict = dict(substitutions)
  sage: subs_dict
  {x_2_2: 4,
   x_0_0: 0,
   x_1_3: 4,
   x_4_0: 4,
   x_4_4: 8,
   x_1_0: 1,
   x_0_1: 1,
   x_4_1: 5,
   x_3_2: 5,
   x_2_3: 5,
   x_4_2: 6,
   x_1_4: 5,
   x_2_0: 2,
   x_3_3: 6,
   x_1_1: 2,
   x_0_4: 4,
   x_0_2: 2,
   x_2_1: 3,
   x_2_4: 6,
   x_4_3: 7,
   x_3_0: 3,
   x_3_4: 7,
   x_1_2: 3,
   x_3_1: 4,
   x_0_3: 3}

Finally, we can pass that dictionary as an argument to "subs" which
performs substitutions on expressions, matrices, and some other things.

  sage: X.subs(subs_dict)
  [0 1 2 3 4]
  [1 2 3 4 5]
  [2 3 4 5 6]
  [3 4 5 6 7]
  [4 5 6 7 8]

Cool, right? Did you ever realize that your example was way too big and
just keep going anyway? So basically,

  * You have to use substitution to stick values into expressions.

  * If you need to do programmatic substitution, a python dictionary
("dict") is often the way to go.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-support] How to create and use lists of variables?

2014-10-25 Thread smohammadh
Thank you Michael, I loaded the file and succeeded to make a list of 
symbols. But now and after evaluating my  expression in terms of its 
elements, how should I assign them different values I want the expression 
to be calculated for? (see a part of my code above, it may illustrate what 
I mean)
Assigning X[1]=0, results to an attribute error:

AttributeError: SymbolSequence instance has no attribute '__setitem__'


-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-support] How to create and use lists of variables?

2014-10-24 Thread Michael Orlitzky
On 10/22/2014 01:22 PM, smohamm...@gmail.com wrote:
> Hello to every one,
> I am a *primitive *sage user and I want to produce a function of n
> variables (|x[1],x[2],...,x[n]|), in nested 'for' loops. I've searched
> in sage's documentation, but I have still 2 problems:
> 1. How to define a list of variables, such that I can use its items, as
> independent variables, in loops. The loops are needed because the
> function is made up of sums of multiplications of clauses of these
> variables?

This isn't built in, but you can use the attached file to do it. There
are detailed examples in the docstring, but basically you load the file
and then create a SymbolSequence object with the name you want:

  sage: load('symbol_sequence.py')
  sage: x = SymbolSequence('x')
  sage: x[0]
  x_0
  sage: x[1]
  x_1
  sage: x[10]
  x_10

Multiple subscripts and slices also work the way you'd want them to (I
hope).


> 2. How to simplify the result of loops according to some extra
> conditions, such as |x[i]^2==1| ?

We have the "assume" function, which can do things like,

  sage: assume(x^2 == 1)

but I don't want to get your hopes up. Even with that assumption, the
expression x^2 won't simplify:

  sage: (x^2).simplify_full()
  x^2

For other equations, it might help though.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.
from sage.all import *

class SymbolSequence:
"""
An iterable object which acts like a sequence of symbolic
expressions (variables).

INPUT:

  - ``name`` -- The sequence name. For example, if you name the
sequence `x`, the variables will be called `x_0`, `x_1`,...

  - ``latex_name`` -- An optional latex expression (string) to
use instead of `name` when converting the symbols to latex.

  - ``domain`` -- A string representing the domain of the symbol,
either 'real', 'complex', or 'positive'.

OUTPUT:

An iterable object containing symbolic expressions.

EXAMPLES:

The simplest use case::

sage: a = SymbolSequence('a')
sage: a[0]
a_0
sage: a[1]
a_1

Create polynomials with symbolic coefficients of arbitrary
degree::

sage: a = SymbolSequence('a')
sage: p = sum([ a[i]*x^i for i in range(0,5)])
sage: p
a_4*x^4 + a_3*x^3 + a_2*x^2 + a_1*x + a_0

Using a different latex name since 'lambda' is reserved::

sage: l = SymbolSequence('l', '\lambda')
sage: l[0]
l_0
sage: latex(l[0])
\lambda_{0}

Using multiple indices::

sage: a = SymbolSequence('a')
sage: a[0,1,2]
a_0_1_2
sage: latex(a[0,1,2])
a_{0}_{1}_{2}
sage: [ a[i,j] for i in range(0,2) for j in range(0,2) ]
[a_0_0, a_0_1, a_1_0, a_1_1]

You can pass slices instead of integers to obtain a list of
symbols::

sage: a = SymbolSequence('a')
sage: a[5:7]
[a_5, a_6]

This even works for the second, third, etc. indices::

sage: a = SymbolSequence('a')
sage: a[0:2, 0:2]
[a_0_0, a_0_1, a_1_0, a_1_1]

TESTS:

We shouldn't overwrite variables in the global namespace::

sage: a = SymbolSequence('a')
sage: a_0 = 4
sage: a[0]
a_0
sage: a_0
4

The symbol at a given index should always be the same, even when
the symbols themselves are unnamed. We store the string
representation and compare because the output is unpredictable::

sage: a = SymbolSequence()
sage: a0str = str(a[0])
sage: str(a[0]) == a0str
True

Slices and single indices work when combined::

sage: a = SymbolSequence('a')
sage: a[3, 0:2]
[a_3_0, a_3_1]
sage: a[0:2, 3]
[a_0_3, a_1_3]

"""

def __init__(self, name=None, latex_name=None, domain=None):
# We store a dict of already-created symbols so that we don't
# recreate a symbol which already exists. This is especially
# helpful when using unnamed variables, if you want e.g. a[0]
# to return the same variable each time.
self._symbols = {}

self._name = name
self._latex_name = latex_name
self._domain = domain


def _create_symbol_(self, subscript):
"""
Return a symbol with the given subscript. Creates the
appropriate name and latex_name before delegating to
SR.symbol().

EXAMPLES::

sage: a = SymbolSequence('a', 'alpha', 'real')
sage: a_1 = a._create_symbol_(1)
sage: a_1
a_1

[sage-support] How to create and use lists of variables?

2014-10-22 Thread smohammadh
Hello to every one,
I am a *primitive *sage user and I want to produce a function of n 
variables (x[1],x[2],...,x[n]), in nested 'for' loops. I've searched in 
sage's documentation, but I have still 2 problems:
1. How to define a list of variables, such that I can use its items, as 
independent variables, in loops. The loops are needed because the function 
is made up of sums of multiplications of clauses of these variables? 
2. How to simplify the result of loops according to some extra conditions, 
such as x[i]^2==1 ?
Any helpful answers would be appreciated.
Thanks for your attentions.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.