Ask Your Question
0

Composition of piecewise function

asked 2020-11-30 16:53:05 +0100

Cyrille gravatar image

updated 2020-11-30 18:47:41 +0100

slelievre gravatar image

[Remark: This is not a question but I do not know how to signal a mistake in the doc. [http://fe.math.kobe-u.ac.jp/icms2010-... uses Piecewise which leads to an error, not piecewise which is good.]

Here is my question:

This code does exactly what I expect

A = 1000
g(x) = x^2

f = piecewise([[[1, 1], (A/12)],
               [[2, 2], (A/(12 - 1) - 1*10)],
               [[3, 3], (A/(12 - 2) - 2*10)]])

show(f(1), ", ", f(2), ", ", f(3))

fg = piecewise([[[1, 1], g(A/12)],
                [[2, 2], g(A/(12 - 1) - 1*10)],
                [[3, 3], g(A/(12 - 2) - 2*10)]])

show(fg(1), ", ", fg(2), ", ", fg(3))

but I have a lot of g(x) to compose with f so I would like to know if there is a way to define a composition of functions for piecewise functions (here defined on subset of ZZ).

There is also the problem where I have a function h which is itself a piecewise function on the same set and I want

fgh = piecewise([[[1, 1], h(1)*g(A/12)],
                 [[2, 2], h(2)*g(A/(12 - 1) - 1*10)],
                 [[3, 3], h(3)*g(A/(12 - 2) - 2*10)]])

show(fg(1), ", ", fg(2), ", ", fg(3))
edit retag flag offensive close merge delete

Comments

This documentation link is obsolete and not official. Use only doc.sagemath.org

FrédéricC gravatar imageFrédéricC ( 2020-11-30 17:07:52 +0100 )edit

I do not know how to signal a mistake in the doc. http://fe.math.kobe-u.ac.jp/icms2010-dvd/SAGE/www.sagemath.org/doc/reference/sage/functions/piecewise.html uses Piecewise which leads to an error, not piecewise which is good.

The documentation you link to is the documentation for Sage 4.5.1, released on 2010-07-19.

It accurately describes how Sage 4.5.1 works.

The piecewise functionality in Sage has since evolved, with some non-backward compatible changes.

Ideally, consult the documentation for the version of Sage you are using.

slelievre gravatar imageslelievre ( 2020-11-30 18:33:42 +0100 )edit

Ideally, provide complete code, for anyone to copy-paste to explore the question.

To make sure of that, paste the commands from your question in a fresh Sage session.

Doing so gives a sense of what others will experience when trying to help.

slelievre gravatar imageslelievre ( 2020-11-30 18:49:47 +0100 )edit

I have rewrited completely my question.

Cyrille gravatar imageCyrille ( 2020-11-30 19:27:52 +0100 )edit

I cannot see the rewritten question.

slelievre gravatar imageslelievre ( 2020-12-01 05:29:00 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2020-12-03 05:46:57 +0100

dsejas gravatar image

Hello, @Cyrille! For this problem, you can exploit the very excellent and comfortable facilities that Sage provides for defining symbolic functions.

In the case of your first piece of code, after you define

A = 1000
g(x) = x^2

f = piecewise([[[1, 1], A/12],
               [[2, 2], A/(12 - 1) - 1*10],
               [[3, 3], A/(12 - 2) - 2*10]])

you just need to define fg(x) = g(f(x)) in order to have the composition. As for your second piece of code, you could do something like fgh(x) = h(x) * fg(x) or, equivalently, fgh(x) = h(x) * g(f(x)).

I hope this helps!

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-11-30 16:53:05 +0100

Seen: 636 times

Last updated: Dec 03 '20