![]() |
[CODE]forstep(x=#v-1,1,[-1],for(i=1,#v-1,[COLOR="Red"]v[i]=v[i+1]-v[i][/COLOR]);v=vector(#v-1,i,v[i]))[/CODE]
came with this today as I lost what i had last night looks like my computer got restarted by my mom as she doesn't know how to turn it from full sleep to awake and with all the data intact. I have made it past this again but I'm having trouble trying to make it figure out the equation lol. |
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] First problem: you're using values before you define them. If, mathematically, [TEX]v_i=v_{i+1}+v_i[/TEX], then you can rearrange this as [TEX]v_{i+1}=0[/TEX] and do [CODE]v[1]=foo; for(i=2,#v, v[i]=0 ); v[/CODE] on the inner loop, where foo is the initial value (I don't know what this would be). 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] The third problem is that you're using v without defining it, twice: once in the outer loop, for the starting value of x, and then in the inner. So actually the code is more like [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])[COLOR="Red"];[/COLOR] , error("_[_]: not a vector.") ) };[/code] where the red semicolon emphasizes that this is *not* output (it's a side-effect). |
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. |
[QUOTE=science_man_88;227026]okay I'll add a V but I wanted it user defined before hand. it calculates sequence of difference then.[/QUOTE]
That's a good idea, but in this case make it a function argument: [code]foo(v,x)={ \\ stuff... };[/code] [QUOTE=science_man_88;227026]maybe I didn't give it a name because i thought you may misinterpret it as you did my last attempt at anything.[/QUOTE] It's a funny thing, as you get better and better with Pari the most difficult part is now usually just explaining what you want rather than coding it. :smile: |
[QUOTE=CRGreathouse;227036]That's a good idea, but in this case make it a function argument:
[code]foo(v,x)={ \\ stuff... };[/code] It's a funny thing, as you get better and better with Pari the most difficult part is now usually just explaining what you want rather than coding it. :smile:[/QUOTE] 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 ? |
[QUOTE=science_man_88;227037]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 ?[/QUOTE]
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. |
sequence of difference like A134458 is for A000043 but as deep as it can to find an equation for the sequence.
|
[QUOTE=science_man_88;227040]sequence of difference like A134458 is for A000043 but as deep as it can to find an equation for the sequence.[/QUOTE]
Ah! Got it. [code]diff(v)={ vector(#v-1,i,v[i+1]-v[i]) }; addhelp(diff, "diff(v): First differences of the vector v.");[/code] Sample: [code]> diff([2,3,5,7,11,13,17,19,23]) %1 = [1, 2, 2, 4, 2, 4, 2, 4][/code] |
[QUOTE=science_man_88;227040]to find an equation for the sequence.[/QUOTE]
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] Example: [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]~[/code] |
too bad it doesn't seem to recognise [1,3,7] as anything.
|
[QUOTE=science_man_88;227048]too bad it doesn't seem to recognise [1,3,7] as anything.[/QUOTE]
That's too short for it to be able to do much analysis. It could construct a recurrence relation for it (with two coefficients), but it's programmed to not do that, since there would be no more terms to check that the relation is correct and not just spurious. For example, it fits the relation 6a(n-1) - 11a(n-2) and the relation 5a(n-1) - 8a(n-2) etc. |
| All times are UTC. The time now is 23:10. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.