Polynomial -> Integrate

11

2

Given a polynomial in one variable with rational coefficients, output an equivalent expression containing only 1, variables, and definite integrals. For example, -x2 can be expressed as ∫x111dtxdu.

E := 1 | var | ∫EEEdvar

Any reasonable input/output method is allowed.

Examples:

\Large 1=1\ \Large x=x\ \Large 0 = \int_1^1 1\text dt\ \Large 2 = \int_{\int_1^{\int_1^1 1\text dv} 1\text du}^1 1\text dt\ \Large x^2=\int_{\int_1^1 1\text dt}^x x\text dv\ \Large \frac 12=\int_{\int_1^1 1\text dt}^1 v\text dv

Your score will be the your code length multiplied by the number of symbols used on the test cases. You should be able to score your program. Lowest score wins.

Test cases:

4/381*x^2+49/8*x^3-17/6
311/59*x^2-92/9*x^3-7/15*x
333/29*x^3+475/96*x^8

Golfing is going to be difficult, because I can't golf just the code or just the output, and I therefore do not know if a change will help or hurt my score until I try it, which sucks in my opinion.

Don't let the score restrict your creation. You are welcomed to answer with mainly one part of the score well optimized, even if the other one badly left.

l4m2

Posted 2018-04-17T03:52:38.210

Reputation: 5 985

related. Duplicate? – Olivier Grégoire – 2018-04-17T08:20:01.737

@OlivierGrégoire Inverse work – l4m2 – 2018-04-17T12:06:41.813

This is an interesting challenge, made much worse by its scoring system. I could instantly claim a score of 0, irregardless of my code's length, simply by assigning chr(8747)(or equivalent) to a variable, and using that as the sign, incurring zero occurrences of the symbol. I'd strongly recommend making this a vanilla code golf challenge. – caird coinheringaahing – 2018-04-17T21:38:53.560

3@cairdcoinheringaahing No matter you use any output format, e.g. 0=[1,1,1], it's still counted as "1 ". Thus you can only get 0 score on test case 1 and x – l4m2 – 2018-04-18T04:31:26.183

1Personally, I think this would be better as a code-golf question. Any solution will be impressive, so I don't think there's a need to require as few integrations as possible. – mbomb007 – 2018-04-23T14:58:49.573

Also, that way you can change the test cases or add more without changing the score of each answer, which I think is really important. What if someone's program doesn't work for input of 0, for example, or -1? Does it need to if the test cases above are the only ones? – mbomb007 – 2018-04-23T16:52:17.830

@mbomb007 https://codegolf.meta.stackexchange.com/a/2507 It's not a pure golf cuz some way to shorten makes the result expression long that's not what I so want

– l4m2 – 2018-04-23T18:19:54.507

@mbomb007 polynomial mean no negative exponent – l4m2 – 2018-04-23T18:20:32.443

@l4m2 The expression is going to be huge no matter what if the only integer we can use is 1... – mbomb007 – 2018-04-23T18:44:00.653

@mbomb007 not that large as you think – l4m2 – 2018-04-23T18:54:14.117

Answers

0

JavaScript (Node.js), 220 bytes * 616 integrals = 135520 score

O=[1,T='t',1,1]
D=(q,t)=>[t,'c',[q,T,1,O],q]
N=n=>n>0?[N(-n),T,1,O]:n?[D(1,1),'c',n&1?[T,T,O,1]:O,N(n/2|0)]:O
P=n=>n?[D(n%2?'x':1,T),T,O,P(n>>1)]:1
F=([e,...s])=>e?[1,T,[F(s),T,'x',O],[N(e[0]),T,O,[P(e[1]-1),'x',O,1]]]:O

Try it online!

enter image description here enter image description here enter image description here

l4m2

Posted 2018-04-17T03:52:38.210

Reputation: 5 985

Equation->Latex: ```function unpack(x) { return x instanceof Array ? \\int_{${unpack(x[2])}}^{${unpack(x[3])}}${unpack(x[0])}\\text d${unpack(x[1])} : x };

console.log (unpack(F([[0, 1], [-7, 15], [311, 59], [-92, 9]])).replace(/{(.)}/g,'$1'));``` – l4m2 – 2018-04-27T02:09:06.160

Just edit your existing answer. We don't need a separate answer for each attempt with a different score. Also, the equation images you included are not even worth including because they're not legible. – mbomb007 – 2018-04-27T18:08:17.610

1@mbomb007 Usually I place similar solution together and quite different ones separated. The previous few times I found some not-allowed symbols used when I get the image, so I keep it here to easier see it's legal – l4m2 – 2018-04-28T05:11:16.960

5

Python 2, 315 bytes * 5113 = 1610595 score

I'm still working on golfing the score. Golfing is going to be difficult, because I can't golf just the code or just the output, and I therefore do not know if a change will help or hurt my score until I try it, which sucks in my opinion.

Despite the annoyance of golfing this, I did enjoy the calculus.

t='t'
Z=lambda n:N(Z(-n))if n<0else[1,t,N(1),Z(n-1)]if n>1else[[1,t,1,1],1][n]
N=lambda a:[1,t,a,Z(0)]
x=lambda n:n>1and[x(n-1),t,Z(0),'x']or'x'
M=lambda a,b:[b,t,Z(0),a]
print reduce(lambda a,b:[1,t,N(a),b],[M((lambda a,b:M(Z(a),[x(b-1)if b>1else 1,'x',Z(0),1]))(*c),x(i)if i else 1)for i,c in enumerate(input())])

Try it online!

Run all test cases - to score, count all [ in the output.

The input polynomial is taken as a list of (numerator, denominator) coefficient pairs in order from lowest to highest power of x. (0, 1) (zero) is used for missing powers.

Output is given with each integral represented by a list [f,t,a,b] to represent ∫abf dt

Verification

Here is a slightly less golfed version that outputs valid Mathematica syntax for integration, which may be tested in an online notebook. Unfortunately, decently-sized programs will not complete in a free notebook.

Go here, scroll to bottom, "Create New Notebook", paste (Wolfram Language Input), and evaluate (Shift+Enter) (Note that using num-pad Enter doesn't work).

Explanation

Uses these equations:

-a = \int_a^{0} 1 ~ dt

n = \int_{-1}^{n-1} 1 ~ dt, n>1

x^n = \int_0^{x} x^{n-1} ~ dt

a+b = \int_{-a}^b 1 ~ dt

ab = \int_0^a b ~ dt

\frac{1}{n} = \int_0^{1} x^{n-1} ~ dx

Links

mbomb007

Posted 2018-04-17T03:52:38.210

Reputation: 21 944

@l4m2 I added the links to the question so we can remove these comments. Thanks. – mbomb007 – 2018-04-24T18:24:48.313

Define Z(n) as def Z(n):return N(Z(-n)) if n<0 else[1,t,1,1] if n<1 else 1 if n<2 else[1,t,N(1),Z(n-1)]? – l4m2 – 2018-04-25T00:32:00.907

or Z=lambda n:N(Z(-n))if n<0else[1,t,N(1),Z(n-1)]if n>1else[[1,t,1,1],1][n] – l4m2 – 2018-04-25T00:34:24.183

1

JavaScript (Node.js), 152 bytes * 5113 integrals = 777176 score

T='t';P=n=>--n?[T,'u',O,P(n)]:1;N=n=>n-1?n>-1?[1,T,N(1-n),1]:[1,T,N(-n),O]:1;O=N(0)
F=([e,...s])=>e?[1,T,[F(s),T,'x',O],[N(e[0]),T,O,[P(e[1]),T,O,1]]]:O

Try it online!

Mainly use these two equations:

\ax+b:=\int_{\int_a^0x\text dt}^b1\text dt\\frac 1a=\int_0^1u^{a-1}\text du

l4m2

Posted 2018-04-17T03:52:38.210

Reputation: 5 985