From e916a8624b6a09737696c124f35059030f0f20e4 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Fri, 10 Apr 2020 23:20:44 +0200 Subject: [PATCH] Initial commit. --- poly.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 poly.py diff --git a/poly.py b/poly.py new file mode 100755 index 0000000..818742b --- /dev/null +++ b/poly.py @@ -0,0 +1,33 @@ +#!/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 + +###################################################################### + +if __name__ == '__main__': + a = torch.tensor([1., 2., 3.]) + b = torch.tensor([2., 5.]) + print(pol_prod(a, b)) + print(pol_prim(b)) -- 2.39.5