Karim Belabas on Thu, 29 Dec 2022 15:17:41 +0100
|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
- To: "Ruud H.G. van Tol" <rvtol@isolution.nl>
- Subject: Re: veccount
- From: Karim Belabas <Karim.Belabas@math.u-bordeaux.fr>
- Date: Thu, 29 Dec 2022 15:16:30 +0100
- Arc-authentication-results: i=1; smail; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1672323390; c=relaxed/relaxed; bh=0tOiKsXhbeUmkdEt86q5H+cW8HNStxJALwmsFl2Ro1U=; h=DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: Mail-Followup-To:References:MIME-Version:Content-Type: Content-Disposition:Content-Transfer-Encoding:In-Reply-To; b=yXVcInkaN+XinS6L5rNQLfnpr5Kl6/npmo+L/6y3q058MvsxQUUKjoUM0DErbbPOb096FgMsGagmCqIA3PxlSFcpdm7mjl1xXoJpaV+QqD8TiWDbeBRwzEqn20jx6pEsrCKpd8fljolaQnb7kATLxBpMENHCyVvrpszegz8tnj0AjHoKnsXKtg7ALm5Akre27fnfCQ+Gx6tk75sJAX6C9BZ2eXU1NEpQ3h3D1pBlCUZduNqosqymdbILv4XAUbSdyaTBcSYGD21l8ZhMNMqB6KMWs4VJoInoETo+8BH/kXj5GLEhnoOqfddJd7G+dkazBKxdjxlis0aOCQpxvr0GhtYOhEqrnjV273CiztPbZfB+D3UMQcUvhxb+7LvriQSOGuq/leUoq7j3VvHfOVabjexP0oER3djKYHz5eWG1Vz8nEE/peF0JaxWIA1e8i/SAdu8u57bMR6VTbo4EkoAVJgh4YX/c8bUkMgYz6jXMiG+93PSD9/Fmxa2Fhx8yJBM7WVbL5eWN3wzeZi2lljrSCi4K/hZ07n4D8Pd7HDETwPRLdKbk+pleacOh6n7bTRRQrru4/B8hc/57bcfVdsni9APK6JRGCKlo9mvL9pp/zVpX25PeW0JPtyiRl4TRYAL2tJOcb3l2aCxaCviE2yoF8SLmjvqcMkJeGUF0q0uVQOA=
- Arc-seal: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1672323390; cv=none; b=iwsXHnMAo0R7F1PmcXuZATdiFpwLWm3ksozWfRDlEHP4LjXMWrRwShq0R5+hw0wh0CiARZKNLN8Vp3xfmMEhePtKc84UF+1hK05BlhX/AZu2oTnr3cG6ztONwAYK1m1GoIthcK+xHro04jr2er8gMR8UkImGM+dqaa/7ehHO+urfBpWZbYhTTgApBgZBKhCESmjZC6yiPQ1sh4O9WhRczzG9gLuGNVcSb1bWNtDdYSyi7XnPWX+1ApJaLrSq4yuF54+d6oDVezM5qXHDT0Y1c/M5q4FglvpcQgCp8e/3fILx5aHfvBOmlWMIn1gvFx0y6W+hP04l+OPQO6IZ1PVW//0QkUa+aH+sLwI5Vkuh0DjMu8hZAHu6NBJ9NTLj1/t5anaLsUQRqmSrKekuvRNPWaVM5yb3/MU2cWlUKOM8bJjAeqYdqyJ3OMXCww6LNSg37CdwQxlVDSuey6dn3Dk7asD+MKNENi0eTesfLg9TV4UIykAeheT4IrkEk0bYnmoSmvmgz+pQKim85PIxbZnF1drRS+5Uj1hmpmJd9zOQB7ax+dM2Jrm+BAvna0M5xAlDNErsDJ5/ywT+ciqR6cnLhgCj8660B9K7hoOYKV/Sv7b36iANQO6d5Joeg70hZ4MDMrLTGcv/2ExZ/TqFTdrWfLoBm2vAcXCHoICAnh6uO/4=
- Authentication-results: smail; arc=none
- Cc: pari-users@pari.math.u-bordeaux.fr
- Delivery-date: Thu, 29 Dec 2022 15:17:41 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=math.u-bordeaux.fr; s=2022; t=1672323390; bh=0tOiKsXhbeUmkdEt86q5H+cW8HNStxJALwmsFl2Ro1U=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=gkkh2SKUkr0nhKqFb26orzXyjOhL2MdLds9ra0lkg8yIYS9Z2OuDXwQWc9YQzB6NM PGPBEHN0mftsx0SvieW0gjtEb9kFdDUT9ryXfMUUNOpyRr6k0I0hizFExWX9LEp3Xm nEHg1M/aaJMvv4WvVRXH39qNFXNBk0jHb3RmfhycHPY4a36NHfeXV05+hvYE1jU1vn KiMgrMzZ6EQKFsXszAdhuYTzUBm8vPcmEHG6dnZc4hkhKcBS4iLbE8J+5rikaMjMAx cemQGE+KMFyoY8Z/XFWmRDYmvS6Bl5CrwmnBdBc3TlD8US+Zp8LqqBe7KQaLwhcPPM FMD7y3/LUbtnIGlPTgfdxrj3FR70QZTTlQnRIW+RT7HAB3lEs49wIOt/cwaNpbT+lX EOTxzIdwqFzysvJAzaCXLNgBYs4Mjw+6fdvQ8OtEh28wqa5YwB/+OcLFFnszJNSoA/ mQDk6ewHyFsrc+0MNOmcSpN0pJ+Utm+ZAoDIXLGw87TooKsUNmqUFnFwTMwOSMumn8 OBXBnzyEdzOkOo+E9si621YV5NVGRiJqZggtqvYaGNEKzWJ6z45t810LJxNOrdhDFF 9ci4mcPj7SsuLC5Grmi/XfpgZ19jMk5c7mtHAwLqW4iJ52wl1YdnsJ1imnErn6Jpmn DZHD1JSppr7Rze+6tk+BOOgQ=
- In-reply-to: <21fedc97-d9b7-42db-7e6f-9fc8ae82eac7@isolution.nl>
- Mail-followup-to: "Ruud H.G. van Tol" <rvtol@isolution.nl>, pari-users@pari.math.u-bordeaux.fr
- References: <cf98b55d-d32b-60c6-6781-a658f2f57e5c@isolution.nl> <Y6c1gaCyEHpztsyR@math.u-bordeaux.fr> <21fedc97-d9b7-42db-7e6f-9fc8ae82eac7@isolution.nl>
* Ruud H.G. van Tol [2022-12-29 13:04]:
> A different case:
>
> ? binary(92)
> %3 = [1, 0, 1, 1, 1, 0, 0]
>
> How to effectively transform that to [1,1,3,2] (run-lengths),
> and next to 3 (the number of different run-length values)?
Note that Rosetta Code includes lots of PARI/GP snippets for standard
programming tasks, e.g.,
https://rosettacode.org/wiki/Run-length_encoding#PARI/GP
> ? {
> my(D= 1, v= binary(92), c= 1, x= v[1],r= List());
> if(D,print(v));
> for(i= 2, #v, if(x == v[i], c++;if(i == #v,listput(r,c)),
> listput(r,c);c=1;x=v[i]));
> r= Vec(r); if(D,print(r));
> r= matreduce(r)[,1]~; if(D,print(r));
> print(#r);
> }
> [1, 0, 1, 1, 1, 0, 0]
> [1, 1, 3, 2]
> [1, 2, 3]
> 3
>
> Only the last result is relevant to A165413,
> so a condensed way from binary(92) to 3 would already help,
Something like
/* assume v is a non-empty vector */
rlev(v) =
{ my(x = v[1], c = 1, L = List());
for (i = 2, #v, if (v[i] == x, c++, listput(L,c); x = v[i]; c = 1));
listput(L,c); #Set(L);
}
? rlev(binary(92))
%2 = 3
? setrand(1); v = vector(10^6, i, random(2));
? rlev(v)
time = 894 ms.
%4 = 20
One can save a log factor by using a counting sort to replace the list L
and final #Set(L):
/* assume v is a non-empty vector */
rlev2(v) =
{ my(x = v[1], c = 1, w = vector(#v));
for (i = 2, #v, if (v[i] == x, c++, w[c] = 1; x = v[i]; c = 1));
w[c] = 1; hammingweight(w);
}
? rlev2(v)
time = 437 ms.
%5 = 20
Cheers,
K.B.
--
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/
`
- References:
- veccount
- From: "Ruud H.G. van Tol" <rvtol@isolution.nl>
- Re: veccount
- From: Karim Belabas <Karim.Belabas@math.u-bordeaux.fr>