1
$\begingroup$

I have the following generating functions:

$l_{2i-1}=l_{1}-(i-2)(w+s)$ with $i\geq 2$ and $l_{2i}=l_{2}-(i-1)(w+s)$ with $i\geq 2$, so the first one is for odd index and the second for even index. Then I need to use the two for a function, let´s say a sum, So i'm going to need $l_{1},l_2,l_3,...$ etc but these are generated from the two different functions, so how can I call the $l_{odd}$ using the first function and $l_{even}$ using the second function in the same sum ?

I tried simply defining my first function like f[2*i_-1] but it doesn´t work :(

Many thanks,

$\endgroup$
1
$\begingroup$

One way is to use OddQ and EvenQ. For example:

m[x_] := x^2 /; OddQ[x]
m[x_] := 5 x^3 /; EvenQ[x]

defines m differently for odd and even arguments. For your f, maybe something like

f[x_] := l1 - (x - 2) (w + s) /; OddQ[x]
f[x_] := l2 - (x - 1) (w + s) /; EvenQ[x]
$\endgroup$
  • 1
    $\begingroup$ g[x_?OddQ] := l1 - (x - 2) (w + s);g[x_?EvenQ] := l2 - (x - 1) (w + s); is also an alternative and maybe even a bit faster. $\endgroup$ – Henrik Schumacher Sep 27 '18 at 22:08
  • $\begingroup$ To actually match the mathematical definition given by the OP, you need to do g[x_?OddQ] := l1 - Quotient[x - 3, 2] (w + s) and g[x_?EvenQ] := l2 - Quotient[x - 2, 2] (w + s). $\endgroup$ – J. M. is away Sep 28 '18 at 0:12
  • $\begingroup$ thank you all ! $\endgroup$ – Felipe Gonzalez Sep 28 '18 at 18:21
  • $\begingroup$ But is any way to add the condition of Odd or Even to the subindex ? Because the subindex "i" in the two functions take Odd and Even values starting from 2, but because of the definition (2i-1) generate the Even indexes and (2i) the odd indexes, $\endgroup$ – Felipe Gonzalez Sep 28 '18 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.