ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 30 Oct 2020 18:40:27 +0100elif in listhttps://ask.sagemath.org/question/54087/elif-in-list/Is it possible to use a recursive list. My problem is that I can code a list with an if condition. But I have not found the way to insert inside a list an `elif`.
For instance in pur Python to code `x[t] = a x[t-1] + b, x[0] given` is coded without any difficulties. But what I search is the list way as in Mathematica (<- I know my mother often told me not tell swear words).
In fact I have two problems the first is purely recursive like mathematica code
RecurrenceTable[{a[n + 1] == 3 a[n], a[1] == 7}, a, {n, 1, 10}]
here the `if` condition would be some thing like
A=[a[n + 1] == 3 a[n] for i in range(10) if n > 1 elif : a[n] == 7 if n in range(0)]
but also
A=[something(i) for i in range(..) if condition1 on something(i) elif condition2 on something(i)]Fri, 30 Oct 2020 09:24:03 +0100https://ask.sagemath.org/question/54087/elif-in-list/Comment by slelievre for <p>Is it possible to use a recursive list. My problem is that I can code a list with an if condition. But I have not found the way to insert inside a list an <code>elif</code>.</p>
<p>For instance in pur Python to code <code>x[t] = a x[t-1] + b, x[0] given</code> is coded without any difficulties. But what I search is the list way as in Mathematica (<- I know my mother often told me not tell swear words).</p>
<p>In fact I have two problems the first is purely recursive like mathematica code</p>
<pre><code>RecurrenceTable[{a[n + 1] == 3 a[n], a[1] == 7}, a, {n, 1, 10}]
</code></pre>
<p>here the <code>if</code> condition would be some thing like</p>
<pre><code>A=[a[n + 1] == 3 a[n] for i in range(10) if n > 1 elif : a[n] == 7 if n in range(0)]
</code></pre>
<p>but also</p>
<pre><code>A=[something(i) for i in range(..) if condition1 on something(i) elif condition2 on something(i)]
</code></pre>
https://ask.sagemath.org/question/54087/elif-in-list/?comment=54089#post-id-54089Please add code that you wish would work but does not work.
From there it should be possible to explain why it does not work
and how to achieve the desired result.
Or add Mathematica code and hopefully someone can translate.
From what I understand the Wolfram Language is publicly specified.Fri, 30 Oct 2020 10:53:48 +0100https://ask.sagemath.org/question/54087/elif-in-list/?comment=54089#post-id-54089Answer by Emmanuel Charpentier for <p>Is it possible to use a recursive list. My problem is that I can code a list with an if condition. But I have not found the way to insert inside a list an <code>elif</code>.</p>
<p>For instance in pur Python to code <code>x[t] = a x[t-1] + b, x[0] given</code> is coded without any difficulties. But what I search is the list way as in Mathematica (<- I know my mother often told me not tell swear words).</p>
<p>In fact I have two problems the first is purely recursive like mathematica code</p>
<pre><code>RecurrenceTable[{a[n + 1] == 3 a[n], a[1] == 7}, a, {n, 1, 10}]
</code></pre>
<p>here the <code>if</code> condition would be some thing like</p>
<pre><code>A=[a[n + 1] == 3 a[n] for i in range(10) if n > 1 elif : a[n] == 7 if n in range(0)]
</code></pre>
<p>but also</p>
<pre><code>A=[something(i) for i in range(..) if condition1 on something(i) elif condition2 on something(i)]
</code></pre>
https://ask.sagemath.org/question/54087/elif-in-list/?answer=54092#post-id-54092Still not have time to learn Python ?
- The Pythonism for the conditional operator is `<value> if <condition> else <alternative value>`.No `elif`, but that's not an hindrance (`elif`is syntactic sugar anyway...).
Example :
sage: [-1 if u<0 else 0 if u==0 else +1 for u in range(-2,3)]
[-1, -1, 0, 1, 1]
- Since a `lambda` function is, by definition, anonymous, there is, as far as I can tell, no way to "cleany" have it call itself.
There are some [proposals](https://stackoverflow.com/questions/481692/can-a-lambda-function-call-itself-recursively-in-python) to do this in contorted ways (amounting to recursively create, use and discard new anonymous functions), which I find difficult to understand *in vivo* and probably impossible to debug.
BTW, aping each and every Mathematica function is probably not a good way to learn Sage ; if you insist on modeling your code on a non-Python language, Lisp would be a much better example...Fri, 30 Oct 2020 18:40:27 +0100https://ask.sagemath.org/question/54087/elif-in-list/?answer=54092#post-id-54092