Karim Belabas on Sun, 07 Jan 2024 17:22:30 +0100
|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: Two questions about vector and matrix manipulation
|
- To: pari-users@pari.math.u-bordeaux.fr
- Subject: Re: Two questions about vector and matrix manipulation
- From: Karim Belabas <Karim.Belabas@math.u-bordeaux.fr>
- Date: Sun, 7 Jan 2024 17:21:59 +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=1704644523; c=relaxed/relaxed; bh=Dk4VazTp+ao2tVR+Dh0cREW5lehUtlFlpViHdNY8zfk=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:Mail-Followup-To: References:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To; b=IC6nHGcBcKIAYUQ14lYRtoO1ncRoTww793UCblE6CKPnJY2KKY2Cyk+jN6kPSGESxAKb4XDbCCHtd1aE2/VmDm7HGzyAWatEcODmEq8m9PrZbvVHV0wd82dsLHzLs1w1QHKnChbDG7i+U4pFxgJmSyutVq8UlAhnHIjfsaNdK/CUoRuLHavF1agYWvEHDsehAFMSdwc9ldJKlriHwOiMXi4mnKopZOjy/qzTg06ckxBiEl9HsCI7/6MXl0gMkcnlEonQFAdr+FKegrKbcf7JRY9JnRb/qs70mBZvx4rhHy7UZqp7oNILnzO2PPZjZz7eorWaMy1xERV9qbPg7Tu71j7E5qEIcQ6NREx8BK8NQ3kytfjM7F1UQrgzFjFQ6TAnnu3pqlM6+QEAcaa8oOlW5k/rbjUwO6UZnKNAJoPe7L+exYnSc59y49yvXHKXmfMBcocdDaz5dczfx6u0cMjmOXN7krB1bljXtDNafdmKJmL9zn3TwM80nax5+uL2t4v/QcVUFfgefwW2L8zsHbqyvxERWSptmVjT6eDb+x0aSZsPm/4t2i4qA2P6zHAH8WNqQuovazhisrWfh9FknU/6klkkVGVTs/WScRdBeLbY7Q/3xrHqfiAZukjHE80SoaF7LQF1HjMK4gDruMtEQjTsLRN8V4SGLRm5elDcXroPzUk=
- Arc-seal: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1704644523; cv=none; b=P7BEw/L0Dp/wCcOSCjACPxIGhIVyvfrMjCbNOnidn0+ORA25IjmZloq/QrrUYHmnIudWD4OSfi/Za5NXt2G6/TtUOUpVdtec0DAT17PnR+dOXpYtpYcbHg73sjsPjmyPniAXmTo0z4+6rO1cqGxf63D9MQ/mXQmv2PqG1O7j0YeoW8NEl+I72/vSaYuwGTSFeGV+J3Lh1VK9TNfaeOo5kIDkvUG0DqcuYT2CaMRAz3UNVdx2NjO/lKAOx68UI+lIHrxUASWQnbybPOCPYOMhb1FE3OmdmH/fy1nUCRNLt1U+Ua0w8bW1ASozoBJsSUy5TmQc++mwxfZU6nq9JaGArcsMzT2cddssyZFY9muLvUHJZIzreAJNLlIJEkBGnracgDitUcU9BXJ/reS/V49heRm5p2kNOQ2PSH/JdJiJDAgheEIjdiAJry1tc15EXSnRbQLTJo4Tav8NpzTEjs4qPtpvJ+WrufrFizrVlBLray4cr2+aGU+p6kx0XQSf6ucCM1hg96lSI9/Ot9rgXgQc7utCviQSF2suuJJcdOFy/bAjgzRievo29RXFjH/pxzIzJeloN8VQOnpdwWqHfFT0/heNpUI++cDtiH7D+WLKBOB2QRKWMlZ9kkOw//YTcvNpEhpTJD679Epybgj9lYnSAy6Py+niS71NUyW8bnpYaag=
- Authentication-results: smail; arc=none
- Delivery-date: Sun, 07 Jan 2024 17:22:30 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=math.u-bordeaux.fr; s=2022; t=1704644523; bh=Dk4VazTp+ao2tVR+Dh0cREW5lehUtlFlpViHdNY8zfk=; h=Date:From:To:Subject:References:In-Reply-To:From; b=qYI0FxNlT8nnJ7ageakULEnN1nFQ7CQUwsyf5ydLNlgANM6r7zNhfy0COT7PrwPUG rYGNsEnE34+e7xLgbhWT6YMdmPutzxuMxe9R+RGPAW5XUbBwTG+trIDsP7d0mmxBGQ /pdmQ3aCBshDPwDFP7+nZM0DdLK8zJR1/O5mvjUQH3BxJ3SAse4jSNwyIo/S17QNps BL6rJ3nYBWjIAwxEhiDAPE/AUnKMIKEvMeEfiDgUNf3rPMdPxWLt7a2E+sfn9DQlND Y6Auk4LIl1qMULLNguxoXKMcsIRSJDijiaF4QcO6tnrtVkjstG8MXXj3gf2ZaT+fJ9 npaiqDu5X5LpRH+DiQz/BYMuChXg1b/0GRTWBYI2UYgajdBz7StvRHcbWPeuju+UPU 5vhFHDSuAmT1ZXfxYwVmRdaV8hAUD5tMjgFJ8r1RJUXqc0UYmm98oFEgZ+/9DeGR4A TxvEr2ESJaWfV6fTYZ5aHQB2agWWyVw+XO56AuJx9yA2XGgzUCdoluKPq2TjWFbTvH oCOebdEkCduRxq/Zg26rlrJHcAiYLAJkoMjWIoy6AR2b5HOUKlOJ9f4Tbdz7Z0GGpE 3j2RKp2JWmeo/yhP1VGg8zVORs0w3GxsWBz2ZoCUdLTrKfJ+6PjNKowRwQI76wva9g yt4xRvs3RTI0ppfjYINLesk8=
- In-reply-to: <ZZqmTBffw+7bgEAT@seventeen>
- Mail-followup-to: pari-users@pari.math.u-bordeaux.fr
- References: <CAEdMSjens8z3O3kzaEONjKjRNvbiPPXv3Xh6kinAg6H94=k=+A@mail.gmail.com> <ZZqmTBffw+7bgEAT@seventeen>
* Bill Allombert [2024-01-07 14:27]:
[...]
> > 2) Suppose I have two vectors of inconsistent length. I want a third vector
> > representing their sum, as if the shorter vector had been padded with zeros
> > like so:
> > f(a,b)=my(k=#a-#b);c=vector(abs(k));if(k>0,b=concat([b,c]),a=concat([a,c]));return(a+b)
> > Is there a better/more elegant way to do this?
>
> Well you can use Vec(...,n) to pad with 0.
> f(a,b)=a=Vec(a,#b);vector(#a,i,a[i]+b[i])
>
> ? f([1,2],[1,2,3])
> %5 = [2,4,3]
If you want the code to work also when b is the shortest vector, you have to
complicate it a little:
f(a,b)=
{
if (#a < #b, a = Vec(a,#b),
#a > #b, b = Vec(b,#a));
vector(#a,i, a[i] + b[i]);
}
Another possibility is
f(a,b) =
{ my (A = #a, B = #b);
vector(max(A,B), i, if (i > A, b[i],
i > B, a[i], a[i] + b[i]));
}
Probably not more elegant, and certainly slower (evaluating a more
complicated expression is more expensive than adding 0)
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/