12
\$\begingroup\$

This is a repost of this challenge, intended to revamp it for looser I/O formats and updated rules

You are to write a program which takes an integer polynomial in \$t\$ as input and outputs the Laplace transform of this polynomial. Some definitions and properties:

  • The Laplace transform of a given function \$f(t)\$ is

$$\mathcal{L}\{f(t)\} = F(s) = \int_0^\infty f(t)e^{-st}dt$$

  • The Laplace transform of \$f(t) = t^n, \, n = 0, 1, 2, ...\$ is

$$\mathcal{L}\{t^n\} = \frac{n!}{s^{n+1}}$$

  • Laplace transforms distribute over addition:

$$\mathcal{L}\{f(t)+g(t)\} = \mathcal{L}\{f(t)\} + \mathcal{L}\{g(t)\}$$

  • The Laplace transform of a constant multiplied by a function equals the constant multiplied by the transform:

$$\mathcal{L}\{af(t)\} = a\mathcal{L}\{f(t)\}$$

  • An integer polynomial is a polynomial where each term has an integer coefficient, and a non-negative order

An worked example:

$$\begin{align} \mathcal{L}\{3t^4+2t^2+t-4\} & = \mathcal{L}\{3t^4\}+\mathcal{L}\{2t^2\}+\mathcal{L}\{t\}-\mathcal{L}\{4\} \\ & = 3\mathcal{L}\{t^4\}+2\mathcal{L}\{t^2\}+\mathcal{L}\{t\}-4\mathcal{L}\{1\} \\ & = 3\left(\frac{4!}{s^5}\right)+2\left(\frac{2!}{s^3}\right)+\left(\frac{1!}{s^2}\right)-4\left(\frac{0!}{s}\right) \\ & = \frac{72}{s^5}+\frac{4}{s^3}+\frac{1}{s^2}-\frac{4}{s} \end{align}$$


You may take input in a standard representation of a polynomial. Some examples (for \$3x^4+2x^2+x-4\$ as an example) are:

  • A list of coefficients. [-4, 1, 2, 0, 3] or [3, 0, 2, 1, -4]
  • Pairs of coefficients and powers. [[3, 4], [2, 2], [1, 1], [-4, 0]] and various different orderings
  • A string, using whatever variable you like. 3x^4+2x^2+x-4

Similarly, as the output will be a polynomial with negative orders, you may output in similar formats, such as (using \$\mathcal{L}\{3x^4+2x^2+x-4\} = \frac{72}{s^5}+\frac4{s^3}+\frac1{s^2}-\frac4s\$):

  • A list of coefficients. [72, 0, 4, 1, -4] or [-4, 1, 4, 0, 72]
  • Pairs of coefficients and powers. [[72, -5], [4, -3], [1, -2], [-4, -1]] and various different orderings (or the positive versions of the powers)
  • A string, using whatever variable you like. 72s^-5+4s^-3+s^-2-4s^-1

If you have an alternative I/O method you're unsure about, please comment below to ask.

This is so the shortest code in bytes wins.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Sandbox. Imaginary brownies for beating my 4-byte Jelly solution \$\endgroup\$ Dec 25, 2020 at 22:56
  • \$\begingroup\$ If we take input as pairs of coefficients and powers, may we have zero coefficient be included as well, like [[3, 4], [0, 3], [2, 2], [1, 1], [-4, 0]]? (Allowing this and the powers being in ascending order would save some languages from extracting indices from the list, for better or worse.) \$\endgroup\$
    – xnor
    Dec 26, 2020 at 0:55
  • 1
    \$\begingroup\$ @xnor This was never intended to be a particularly difficult challenge, so I see no real harm in allowing that \$\endgroup\$ Dec 26, 2020 at 0:57

18 Answers 18

7
\$\begingroup\$

Haskell, 25 bytes

zipWith(*)$scanl(*)1[1..]

Try it online!

Pretty straightforward: Generates the list of factorials [1,1,2,6,...] with a scanl, then does zipWith(*) to multiply each element of the input by the corresponding value.

32 bytes

foldr(\(i,x)r->x:map((i+1)*)r)[]

Try it online!

A pretty folding-based solution. Takes inputs as (exponent, coefficient) pairs.

\$\endgroup\$
6
\$\begingroup\$

convey, 15 bytes

v"*<
0+1"
 1{*}

Try it online!

run

The two left columns copy " 1, 2, 3, … into the top *. The value in the top right gets multiplied by that every lap, so we get (starting with an extra 1 = 0!) 1!, 2!, 3!, … copied into the bottom *. { reads the input, multiplies it with the factorials and outputs them }.

\$\endgroup\$
5
\$\begingroup\$

Jelly, 4 bytes

J’!×

Takes input as list of coefficients.

Explanation

J’!×

J   | Returns an array of elements from 1 to length of input array
 ’  | Subtracts 1 from each
  ! | Factorial each
   ×| Multiply each item in the original array by the created array

Try it online!

\$\endgroup\$
5
\$\begingroup\$

APL (Dyalog Unicode), 3 bytes

×∘!

Try it online!

Takes the liberal I/O to the extreme: takes the polynomial \$ 3x^4 + 2x^2+x-4 \$ as two arguments, the coefficients on the left and the powers on the right in decreasing order and including zero terms, as in 3 0 2 1 ¯4 f 4 3 2 1 0. Returns the polynomial as a vector of coefficients.

\$\endgroup\$
4
\$\begingroup\$

PowerShell, 28 bytes

Input as a list of coefficients

$p++;$args|%{$p*$_;$p*=++$i}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog Unicode), 7 bytes

⊢×!∘⍳∘≢

Try it online!

Uses ⎕IO←0 (0-indexing)

Input as a list of coefficients.

\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 10 bytes

#2!#&@@@#&

Try it online!

Input a list of coefficient/power pairs, including zero coefficients, sorted by power, and output a list of corresponding coefficients.


The built-in is longer: 23 bytes

LaplaceTransform[#,t,]&

Try it online!

Input a polynomial in terms of t, and output one in terms of Null.

\$\endgroup\$
2
\$\begingroup\$

Retina, 30 bytes

L$`.+
$&$:&*
+`\d+_
$.(*$(_$%'

Try it online! I/O is a newline-delimited list of coefficients from lowest to highest degree. Explanation:

L$`.+
$&$:&*

For each coefficient, append a number of underscores equal to its degree.

+`\d+_
$.(*$(_$%'

Until no underscores remain, multiply each coefficient by the number of following underscores, deleting one in the process.

\$\endgroup\$
2
\$\begingroup\$

Scala 3, 52 48 bytes

p=>p.indices.scanLeft(1)(_*_.+(1))zip p map(_*_)

Try it online!

Input and output as a list of integers, from lowest to highest degree.

p.indices gives us a range from 0 to p.size - 1. Scanning left with multiplication gives the factorial at each index, but since the first element is 0, we need to add 1 (hence _.+(1)). Then all the factorials are zipped with the coefficients and multiplied together.

\$\endgroup\$
2
\$\begingroup\$

Python 2, 39 bytes

p=i=1
while 1:print p*input();p*=i;i+=1

Try it online!

Input and output are coefficients, one per line, starting with smallest degree (nearest zero).

Taking in (coefficient, exponent) pairs turns out slightly longer.

p=1
while 1:x,i=input();print p*x;p*=i+1

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Raku, 15 bytes

*Z*1,|[\*] 1..*

Try it online!

[\*] 1..* is the infinite sequence of factorials starting with 1!. An additional 1 (for 0!) is pasted on to the front, then the whole thing is zipped-with-multiplication (Z*) with the sole input sequence *.

\$\endgroup\$
2
\$\begingroup\$

Japt -m, 3 bytes

*Vl

Try it here

\$\endgroup\$
2
\$\begingroup\$

R, 34 28 25 bytes

(x=scan())*gamma(seq(!x))

Try it online!

Pretty straightforward.
R lacks a short-named factorial function, but has gamma.
Generates sequence along x using trick from @Giuseppe.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6),  31  29 bytes

I/O: lists of coefficients, from lowest to highest degree.

a=>a.map((v,i)=>v*=p=i?p*i:1)

Try it online!

Commented

a =>              // a[] = polynomial coefficients
  a.map((v, i) => // for each coefficient v at position i in a[]:
    v *=          //   multiply v by:
      p =         //     the updated factorial p, which is:
        i ?       //       if i > 0:
          p * i   //         multiplied by i
        :         //       else:
          1       //         initialized to 1
  )               // end of map()
\$\endgroup\$
2
\$\begingroup\$

SageMath, 27 23 bytes

Saved 4 bytes thanks to ovs!!!

lambda f:f.laplace(x,x)

Try it online!

Takes a function of \$x\$ as input and returns the Laplace transform as a function of \$x\$.

\$\endgroup\$
3
  • \$\begingroup\$ time to make a golfing language using sagemath \$\endgroup\$
    – Razetime
    Dec 26, 2020 at 5:47
  • \$\begingroup\$ f.laplace(x,x) saves a few bytes. \$\endgroup\$
    – ovs
    Dec 26, 2020 at 7:51
  • \$\begingroup\$ @ovs Oh, very nice - thanks! :D \$\endgroup\$
    – Noodle9
    Dec 26, 2020 at 11:48
1
\$\begingroup\$

Charcoal, 12 bytes

IEA×ιΠ⊞Oυ∨κ¹

Try it online! Link is to verbose version of code. I/O is a list of coefficients from lowest to highest degree. Explanation:

  A             Input array
 E              Map over elements
    ι           Current element
   ×            Multiplied by
     Π          Product of
        υ       Predefined empty list
      ⊞O        After pushing
         ∨      Logical Or of
          κ     Current index
           ¹    Literal 1
I               Cast to string
                Implicitly print
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 4 bytes

εN!*

Try it online.

Or alternatively:

ā<!*

Try it online.

Both take a list of coefficients as input.

Explanation:

ε     # Map over each value of the (implicit) input-list
 N    #  Push the 0-based map-index
  !   #  Pop and take it's faculty
   *  #  Multiply it by the current value
      # (after the map, the resulting list is output implicitly)

ā     # Push a list in the range [1,length] based on the (implicit) input-list
 <    # Decrease each by 1 to make the range [0,length)
  !   # Take the faculty of each
   *  # And multiply it to the values at the same positions in the (implicit) input-list
      # (after which the result is output implicitly)
\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 10 bytes

A built-in.

serlaplace

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I‘m not quite sure what the output format is? 3x^4+2x^2+x-4 should output something like 72s^-5+4s^-3+s^-2-4s^-1 \$\endgroup\$ Feb 9, 2021 at 0:14
  • \$\begingroup\$ @cairdcoinheringaahing Input and output use the built-in polynomial type. Internally they are just a list of coefficients. \$\endgroup\$
    – alephalpha
    Feb 9, 2021 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.