![]() |
|
|
#859 |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
Code:
forstep(x=#v-1,1,[-1],for(i=1,#v-1,v[i]=v[i+1]-v[i]);v=vector(#v-1,i,v[i])) Last fiddled with by science_man_88 on 2010-08-25 at 12:01 |
|
|
|
|
|
#860 |
|
Aug 2006
3×1,993 Posts |
Well, I don't know what the code is supposed to do, but I'll have a look.
Code:
forstep(x=#v-1,1,-1,
for(i=1,#v-1,
v[i]=v[i+1]-v[i]
);
v=vector(#v-1,i,v[i])
)
Code:
v[1]=foo; for(i=2,#v, v[i]=0 ); v The second problem is that the outer loop doesn't do anything with the values of v that you calculate, and the inner loop doesn't use the x. This makes your code essentially Code:
nop(x)={
};
Code:
foo(x)={
if(type(v) == "t_VEC" || type(v) == "t_COL",
for(i=1,#v-1,v[i]=v[i+1]-v[i]);
v=vector(#v-1,i,v[i]);
,
error("_[_]: not a vector.")
)
};
|
|
|
|
|
|
#861 |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
okay I'll add a V but I wanted it user defined before hand. it calculates sequence of difference then.
maybe I didn't give it a name because i thought you may misinterpret it as you did my last attempt at anything. Last fiddled with by science_man_88 on 2010-08-25 at 13:33 |
|
|
|
|
|
#862 | ||
|
Aug 2006
3×1,993 Posts |
Quote:
Code:
foo(v,x)={
\\ stuff...
};
Quote:
|
||
|
|
|
|
|
#863 |
|
"Forget I exist"
Jul 2009
Dumbassville
20C016 Posts |
hilarious! not. I think for the finding the equation i can store the value i get at the level as coefficients in a Vec and subtract it from a vector like the original v and then do the loop again so this may take another loop yet got a faster way ?
|
|
|
|
|
|
#864 |
|
Aug 2006
3×1,993 Posts |
I don't know what you're doing, so I can't speak to whether this is a correct or an efficient way to do what you want. But Pari can do this, certainly.
|
|
|
|
|
|
#865 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
sequence of difference like A134458 is for A000043 but as deep as it can to find an equation for the sequence.
|
|
|
|
|
|
#866 | |
|
Aug 2006
597910 Posts |
Quote:
Code:
diff(v)={
vector(#v-1,i,v[i+1]-v[i])
};
addhelp(diff, "diff(v): First differences of the vector v.");
Code:
> diff([2,3,5,7,11,13,17,19,23]) %1 = [1, 2, 2, 4, 2, 4, 2, 4] |
|
|
|
|
|
|
#867 |
|
Aug 2006
3·1,993 Posts |
I have some code for something like that. It's ugly, and it doesn't always work, but try this:
Code:
findrec(v:vec, verbose:bool=1)={
my(c,d = (#v - 1) >> 1, firstNonzero = 0);
if (#v < 3,
error("findrec: Not enough information to determine if the sequence is a recurrence relation: matrix is underdetermined. Give more terms and try again.")
);
forstep(i=d,1,-1,
if(v[i] != 0, firstNonzero = i)
);
if(firstNonzero == 0,
if(verbose,
print1("Recurrence relation is a(n) = 0.");
);
return([0]~);
);
for (i=firstNonzero,d,
c = findrecd(v,i,verbose);
if(c, return(c))
);
if(verbose,print("Cannot be described by a homogeneous linear recurrence relation with "d" or fewer coefficients."));
0
};
addhelp(findrec, "findrec(v, {verbose=1}): Tries to find a homogeneous linear recurrence relation with constant coefficients to fit the vector v.");
findrecd(v:vec, d:int, verbose:bool=1)={
my(M,c);
if (#v < 2*d,
error("findrec: Not enough information to determine if the sequence is a "d"-coefficient recurrence relation; matrix is underdetermined. Give more terms or reduce d and try again.")
);
M = matrix(d,d,x,y,v[d+x-y]);
\\print(M" * c = "vector(d,i,v[d+i])~);
c = matsolve(M,vector(d,i,v[d+i])~);
for(n=2*d+1,#v,
if(v[n] != sum(i=1,d,v[n-i] * c[i]),return(0))
);
if(verbose,
my(init=1,s);
print1("Recurrence relation is a(n) = ");
for(i=1,#c,
if(c[i] == 0, next);
if(init,
s = initial(c[i], Str("a(n-", i, ")"));
init = 0
,
s = Str(s, medial(c[i], Str("a(n-", i, ")")))
)
);
print(s".");
if((vecmax(c) == 1 && vecmin(c) == 0 && vecsum(c) == 1) || c == [1]~,
print("Sequence has period "#c".");
,
my(g=0);
for(i=1,#c,
if(c[i] != 0, g = gcd(g, i))
);
if (g > 1,
my(gvec = vector(#c/g, i, c[i*g]),s,init=1);
for(i=1,#gvec,
if(gvec[i] == 0, next);
if(init,
s = initial(gvec[i], Str("a(n - ", i, ")"));
init = 0
,
s = Str(s, medial(gvec[i], Str("a(n - ", i, ")")))
)
);
print("Can be though of as "g" interlocking sequences, each of the form a(n) = "s".")
)
);
print((#v-2*d)" d.f.")
);
c
};
addhelp(findrecd, "findrecd(v, d, {verbose=1}): Helper function for findrec. Tries to find a d-coefficient homogeneous linear recurrence relation with constant coefficients to fit the vector v.");
initial(n:int, s:str)={
if (n == 0, return(""));
if (n == -1, return(Str("-", s)));
if (n == 1, return(s));
Str(n, s)
};
medial(n:int, s:str)={
if (n == 0, return(""));
if (n == -1, return(Str(" - ", s)));
if (n == 1, return(Str(" + ", s)));
Str(if (n < 0, " - ", " + "), abs(n), s)
};
Code:
> findrec([1,1,2,3,5,8,13]) Recurrence relation is a(n) = a(n-1) + a(n-2). 3 d.f. %1 = [1, 1]~ |
|
|
|
|
|
#868 |
|
"Forget I exist"
Jul 2009
Dumbassville
838410 Posts |
too bad it doesn't seem to recognise [1,3,7] as anything.
|
|
|
|
|
|
#869 | |
|
Aug 2006
3×1,993 Posts |
Quote:
For example, it fits the relation 6a(n-1) - 11a(n-2) and the relation 5a(n-1) - 8a(n-2) etc. |
|
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Why do I sometimes see all the <> formatting commands when I quote or edit? | cheesehead | Forum Feedback | 3 | 2013-05-25 12:56 |
| Passing commands to PARI on Windows | James Heinrich | Software | 2 | 2012-05-13 19:19 |
| Ubiquity commands | Mini-Geek | Aliquot Sequences | 1 | 2009-09-22 19:33 |
| 64-bit Pari? | CRGreathouse | Software | 2 | 2009-03-13 04:22 |
| Are these commands correct? | jasong | Linux | 2 | 2007-10-18 23:40 |