From: Francois Fleuret Date: Fri, 10 Apr 2020 21:18:21 +0000 (+0200) Subject: Initial commit. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=488798e2df8845d1991243d26053eec2d5031b75;p=python.git Initial commit. --- diff --git a/poly.py b/poly.py new file mode 100755 index 0000000..ae9f88e --- /dev/null +++ b/poly.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +# Any copyright is dedicated to the Public Domain. +# https://creativecommons.org/publicdomain/zero/1.0/ + +# Written by Francois Fleuret + +import torch + +def pol_prod(a, b): + m = a[:, None] * b[None, :] + mm = m.new() + mm.set_(m.storage(), 0, (m.size(0), m.size(0) + m.size(1) - 1), (m.size(1) - 1, 1)) + k = torch.arange(a.size(0))[:, None] + torch.arange(b.size(0))[None, :] + kk = k.new() + kk.set_(k.storage(), 0, (k.size(0), k.size(0) + k.size(1) - 1), (k.size(1) - 1, 1)) + q = (kk == torch.arange(a.size(0) + b.size(0) - 1)[None, :]) + return (mm * q).sum(0) + +def pol_prim(a): + n = torch.arange(a.size(0) + 1).float() + n[1:] = a / n[1:] + return n + +a = torch.tensor([1., 2., 3.]) +b = torch.tensor([2., 5.]) + +print(pol_prod(a, b)) +print(pol_prim(b))