Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
0

Extending piecwise to 2 variables (or a variable and a parameter)

asked 4 years ago

Cyrille gravatar image

updated 4 years ago

This piecewise function work correctly

score =  piecewise([([i,i], ncand - i) for i in range(ncand)])

because ncand is defined earlier in my notebook. when I typeset say score(1) It returns what I expect. But I would like that ncand be a variable.

Preview: (hide)

Comments

score can only be evaluated in the integers 0,1,2,...,ncand1 and, in fact, score(i)=ncandi. Is it intended? Anyway, if you have something like

ncand = some value
score = piecewise(list of cases depending on ncand)

you can redefine score to depend on ncand as follows:

def score(x,ncand):
    s = piecewise(list of cases depending on ncand)
    return s(x)
Juanjo gravatar imageJuanjo ( 4 years ago )

Juanjo it was intended

Cyrille gravatar imageCyrille ( 4 years ago )

But you answer is not self evident. Even short it ananswer not a comment.

Cyrille gravatar imageCyrille ( 4 years ago )

I have transformed my comment into an answer.

Juanjo gravatar imageJuanjo ( 4 years ago )

1 Answer

Sort by » oldest newest most voted
0

answered 4 years ago

Juanjo gravatar image

Define score as follows:

def score(x, ncand=10):
    s = piecewise([([i,i], ncand - i) for i in range(ncand)])
    return s(x)

Then you can use score as in the following examples:

sage: score(7)
3
sage: score(3, ncand=7)
4
sage: score(3,7)
4

Observe that, if omitted, the value of ncand is 10.

Preview: (hide)
link

Comments

I was aware of the default mechanism. But in all cases thanks for uour answer.

Cyrille gravatar imageCyrille ( 4 years ago )

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: 4 years ago

Seen: 282 times

Last updated: Dec 22 '20