Ask Your Question
3

shift picewise functions

asked 2011-11-03 11:15:34 +0100

sagefan gravatar image

Suppose I define a piecewise function f for example:

f = Piecewise([[(-infinity,1),1],[(1,+infinity),x]])

How to define the shifted function g with g(x) = f(x-2) for all x?

Or more generally: If h is another function. How to define $g = f\circ h$ in sage?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

answered 2012-10-16 23:49:41 +0100

rbeezer gravatar image

The following works as expected:

f1(x)=0
f2(x)=exp(-1/(x^2))
f=Piecewise([[(-10,0),f1],[(0,10),f2]])
h(x)=x-2
c = compose(f, h)
plot(c, -8, 12)
edit flag offensive delete link more

Comments

Wow, I had no idea that existed. Nice!

kcrisman gravatar imagekcrisman ( 2012-10-17 09:08:43 +0100 )edit
2

answered 2011-11-03 11:58:40 +0100

kcrisman gravatar image

To answer your second question:

sage: f(x) = x^2+5
sage: g(x) = 25*sqrt(x+1)
sage: g(f(x))
25*sqrt(x^2 + 6)
sage: f(g(x))
625*x + 630
sage: h(x) = f(g(x))
sage: h
x |--> 625*x + 630

The first one is harder, because Piecewise functions were implemented long before we even had symbolic functions, and hence are missing a lot of functionality. This is a long-standing need, but it takes a certain expertise in both the old and new code, as well as Ginac, to do this.

(To the cognoscenti - sympy seems to have piecewise functions, but I can't find much documentation, just obscure hints that they are there. How powerful are theirs?)

edit flag offensive delete link more

Comments

So there is no workaround to compose piecewise-defined functions?

sagefan gravatar imagesagefan ( 2011-11-04 16:41:47 +0100 )edit

There may be, but I can't think of one offhand. That doesn't mean it doesn't exist, just that I can't think of it right now.

kcrisman gravatar imagekcrisman ( 2011-11-04 22:53:20 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-11-03 11:15:34 +0100

Seen: 782 times

Last updated: Oct 16 '12