Ask Your Question

math4tots's profile - activity

2021-11-01 10:59:16 +0200 received badge  Notable Question (source)
2021-01-15 02:10:36 +0200 received badge  Popular Question (source)
2018-04-16 20:08:18 +0200 received badge  Notable Question (source)
2016-09-05 19:00:39 +0200 received badge  Nice Question (source)
2016-09-05 11:50:57 +0200 received badge  Student (source)
2016-09-05 11:33:40 +0200 asked a question sequence of functions

How do I define a sequence of functions in Sage?

I'd like to do something like

f(0)(a, b) = a * b
f(n)(a, b) = a + b * f(n-1)(a+b, a-b)

Such that when I use it, I could e.g. solve for x in

f(3)(x, x+1) = 1
2016-09-05 11:33:40 +0200 asked a question Anonymous symbolic functions?

I'd like to be able to define symbolic functions recursively, for instance like the following:

f(0)(a, b) = a * b
f(n)(a, b) = f(n-1)(a + 2*b, a - b)

g = f(5)

print(g(1, 2))

The above program doesn't run because "f(...)(...) = <expression>" isn't valid sage code.

How do I achieve something similar in sage?

Essentially I want:

f(0)(a, b) = a * b
f(1)(a, b) = (a+b) * (a-b)
f(2)(a, b) = ((a+b) + (a-b)) * ((a+b) - (a-b))
...

I am not very familiar with Sage, and am open to different ways of approaching this. Is there some other abstraction I should look into when I want to work with a recursively defined sequence of functions?

Thank you!

2015-01-14 14:14:40 +0200 received badge  Popular Question (source)
2012-04-29 06:35:26 +0200 received badge  Supporter (source)
2012-04-29 06:35:25 +0200 marked best answer How do I find the subsets of given Set?

You don't have a set to begin with. CartesianProduct returns a special object. That object automatically converts to a list of lists when you pass it to Subsets. Then Subsets goes through that list of lists, and gets confused by the lists inside - it cannot make subsets out of them, because lists are not hashable. One way around is to use tuples instead of lists:

def Z(m,n):
    return CartesianProduct(IntegerRange(m),IntegerRange(n))
print Subsets(map(tuple,Z(2,2)))
2012-04-29 06:35:25 +0200 received badge  Scholar (source)
2012-04-29 06:07:57 +0200 received badge  Editor (source)
2012-04-29 06:03:16 +0200 asked a question How do I find the subsets of given Set?

I have code that looks like this:

def Z(m,n):
  return CartesianProduct(IntegerRange(m),IntegerRange(n))

print Subsets(Z(2,2))

However, when I try to run it, it gives me the following scary error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/math4tots/math/<ipython console> in <module>()

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/combinat/combinat.pyc in __str__(self)
   1052             'Partitions of the integer 5'
   1053         """
-> 1054         return self.__repr__()
   1055     
   1056     def _repr_(self):

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/combinat/subset.pyc in __repr__(self)
    152             'Subsets of {1, 2, 3}'
    153         """
--> 154         return "Subsets of %s"%self.s
    155 
    156     def __contains__(self, value):

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.SageObject.__repr__ (sage/structure/sage_object.c:1492)()

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/sets/set.pyc in _repr_(self)
    673             {0, 1}
    674         """
--> 675         s = repr(self.set())
    676         return "{" + s[5:-2] + "}"
    677 

/opt/sage-4.8-linux-64bit-ubuntu_10.04.3_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/sets/set.pyc in set(self)
    717             <class 'sage.sets.set.Set_object_enumerated_with_category'>
    718         """
--> 719         return set(self.object())
    720 
    721     def frozenset(self):

TypeError: unhashable type: 'list'

What did I do wrong? What is the canonical way to handle this situation?

I was expecting an output something along the lines of "Subsets of Cartesian Product of {0..2} {0..2}"