mersenneforum.org  

Go Back   mersenneforum.org > Math Stuff > Computer Science & Computational Number Theory > PARI/GP

Reply
 
Thread Tools
Old 2010-08-25, 11:40   #859
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

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]))
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.

Last fiddled with by science_man_88 on 2010-08-25 at 12:01
science_man_88 is offline   Reply With Quote
Old 2010-08-25, 13:28   #860
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

175B16 Posts
Default

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])
)
First problem: you're using values before you define them. If, mathematically, v_i=v_{i+1}+v_i, then you can rearrange this as v_{i+1}=0 and do
Code:
v[1]=foo;
for(i=2,#v,
  v[i]=0
);
v
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)={
  
};
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]);
  ,
    error("_[_]: not a vector.")
  )
};
where the red semicolon emphasizes that this is *not* output (it's a side-effect).
CRGreathouse is offline   Reply With Quote
Old 2010-08-25, 13:31   #861
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

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
science_man_88 is offline   Reply With Quote
Old 2010-08-25, 15:50   #862
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
okay I'll add a V but I wanted it user defined before hand. it calculates sequence of difference then.
That's a good idea, but in this case make it a function argument:
Code:
foo(v,x)={
  \\ stuff...
};
Quote:
Originally Posted by science_man_88 View Post
maybe I didn't give it a name because i thought you may misinterpret it as you did my last attempt at anything.
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.
CRGreathouse is offline   Reply With Quote
Old 2010-08-25, 15:59   #863
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
That's a good idea, but in this case make it a function argument:
Code:
foo(v,x)={
  \\ stuff...
};


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.
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 ?
science_man_88 is offline   Reply With Quote
Old 2010-08-25, 16:07   #864
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
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 ?
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.
CRGreathouse is offline   Reply With Quote
Old 2010-08-25, 16:12   #865
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

sequence of difference like A134458 is for A000043 but as deep as it can to find an equation for the sequence.
science_man_88 is offline   Reply With Quote
Old 2010-08-25, 16:40   #866
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3·1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
sequence of difference like A134458 is for A000043 but as deep as it can to find an equation for the sequence.
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.");
Sample:
Code:
> diff([2,3,5,7,11,13,17,19,23])
%1 = [1, 2, 2, 4, 2, 4, 2, 4]
CRGreathouse is offline   Reply With Quote
Old 2010-08-25, 16:44   #867
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
to find an equation for the sequence.
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)
};
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]~
CRGreathouse is offline   Reply With Quote
Old 2010-08-25, 17:02   #868
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

too bad it doesn't seem to recognise [1,3,7] as anything.
science_man_88 is offline   Reply With Quote
Old 2010-08-25, 17:12   #869
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

135338 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
too bad it doesn't seem to recognise [1,3,7] as anything.
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.
CRGreathouse is offline   Reply With Quote
Reply

Thread Tools


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

All times are UTC. The time now is 23:10.


Fri Aug 6 23:10:13 UTC 2021 up 14 days, 17:39, 1 user, load averages: 4.68, 4.11, 3.99

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.