Karim Belabas on Wed, 11 Dec 2024 22:28:56 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Is this the way to use auxiliary variables in vector initialized via multiple iterators?


* Karim Belabas [2024-12-11 22:24]:
> * hermann@stamm-wilbrandt.de [2024-12-11 22:16]:
> > Doc talks about
> > https://pari.math.u-bordeaux.fr/pub/pari/manuals/2.17.0/users.pdf#page=25
> > 
> > "To iterate over more than one variable, you may separate clauses with ; as
> > in"
> > 
> > I found no mentioning how to introduce auxiliary variables to avoid
> > repetition of terms.
> > 
> > I wanted to use auxiliary variable s for sum p^2+4*q^2, and this does the
> > job:
> > 
> > ? P=primes([2,100]);vecmin([s|p<-P;q<-P;s<-[p^2+4*q^2],isprime(s)])
> > 41
> > ?
> > 
> > Is there another way of assignment to s without having to introduce
> > auxiliary single element set?
> 
> P = primes([2,100]); vecmin([s | p<-P;q<-P, isprime(s = p^2+4*q^2)])

Another possibility is the more general 'if (true,)' hack to introduce
an arbitrary sequence of instructions, where a single one is allowed:

  vecmin([s | p<-P;q<-P, if (1, s = p^2+4*q^2; isprime(s))])

Cheers,

    K.B.
-- 
Pr. Karim Belabas, U. Bordeaux, Vice-président en charge du Numérique
Institut de Mathématiques de Bordeaux UMR 5251 - (+33) 05 40 00 29 77
http://www.math.u-bordeaux.fr/~kbelabas/