I am programming a small game (with other programming language) in which I have to use Mod
function to express piecewise linear period functions.
In my case, two hard parts I think exist.
(1) The periodic function is linear in the piecewise sence, like the function below. In a full period it is NOT linear.
(2) There are NO powerful function like Piecewise
function in Mathematica, and all I can use are some very basic functions including Mod
, +
,-
, *
, \
, Abs
, Floor
, Ceiling
,Exp
,10^
,Sin
,Cos
,Tan
,ASin
,ACos
, ATan
,Log
and logic control commands If
, If Then Else
.
(3) After many times trying, I also found another challenging part is related to Mod
of a negative number .My piecewise linear periodic function can thake negative integers whose Mod
is so confusing to me. I can calculate the Mod
of a negative number by hand, but I have no clear idea how it relates to a repeat pattern.
So is it possible to use Mathematica to find something like linear combinations of Mod
functions for any periodic piecewise linear function?
For example, the periodic piecewise linear period function like this:
f[x_?IntegerQ] :=
Piecewise[{{-10 x + 2, 1 <= x < 4}, {-5 x + 8, 4 <= x <= 10}}, True]
f[x_?IntegerQ] := f[x + 10] /; x < 1
f[x_?IntegerQ] := f[x - 10] /; x > 10
ListPlot[Table[{x, f[x]}, {x, -9, 20}]]
Any advices are highly appreciated! Thanks :)
period = 10; ListPlot[Table[{x, Mod[x, period, 0]}, {x, -9, 20}]]
? $\endgroup$mod()
function work exactly likeMod[]
? I doubt it: BothMod[x, m]
andMod[x, m, x0]
?Mod[x + m, m] == Mod[x, m]
, even when-m < x < 0
? It seems necessary to know how the functions in both Mathematica and the target language work to know how to solve the problem. $\endgroup$Mod[x, m, x0]
andMod[x + m, m] == Mod[x, m]
. My naive idea is to use a guess form ofMod
linear combination to fit the data generated by original piecewise linear function. I will think more on your great answer. Thanks :) $\endgroup$