Symbolic Integration of Polynomials
MATLAB, 646 x 0.9 = 581.4 bytes
t=input('','s');p=char(960);s=regexprep(t,{p,'pi([a-zA-Z])','([a-zA-Z])pi','([\)e\d])([a-zA-Z])','([a-zA-Z])(([\(\d]|pi))','e^(\(.+?\))','e'},{'pi','pi*$1','$1*pi','$1*$2','$1*$2','exp($1)','exp(1)'});r=[s(regexp(s,'\<[a-zA-Z]\>')),'x'];r=r(1);e=0;try
I=int(sym(strsplit(s,' + ')),r);S=[];for i=I
S=[S char(i) ' + '];end
b=0;o=[];for i=1:nnz(S)
c=S(i);b=b+(c==40)-(c==41);if(c==42&&S(i+1)==r)||(b&&c==32)
c='';end
o=[o c];end
o=regexprep(char([8747 40 t ')d' r ' = ' o 67]),{'pi','exp\(1\)','exp','\^([^\(])',['1/' r]},{p,'e','e^','^($1)',[r '^(-1)']});catch
e=1;end
if e||~isempty(strfind(o,'log'))
disp(['Cannot integrate ' t]);else
disp(o);end
This is currently a work-in-progress using MATLABs built in symbolic integration capabilities. Currently the requirements have been updated so the format now matches the requirements. It also does qualify for the second -10% bonus.
If anyone wants to pitch in and suggest ways of correcting the output, or use this code as a basis for another answer, feel free :). If I can find the time, I'll keep playing with it and see if I can think how to reformat the output.
Update: Ok, so after a bit more work, here is how the code currently stands. It is still a work in progress, but now getting closer to matching the required output.
t=input('','s'); %Get input as a string
p=char(960); %Pi character
s=regexprep(t,{p,'pi([a-zA-Z])','([a-zA-Z])pi','([\)e\d])([a-zA-Z])','([a-zA-Z])(([\(\d]|pi))','e^(\(.+?\))','e'},{'pi','pi*$1','$1*pi','$1*$2','$1*$2','exp($1)','exp(1)'}); %Reformat input to work with built in symbolic integration
r=[s(regexp(s,'\<[a-zA-Z]\>')),'x'];r=r(1); %determine the variable we are integrating
e=0; %Assume success
try
I=int(sym(strsplit(s,' + ')),r); %Integrate each term seperately to avoid unwanted simplificaiton
S=[];
for i=I
S=[S char(i) ' + ']; %Recombine integrated terms
end
%Now postprocess the output to try and match the requirements
b=0;o=[];
for i=1:nnz(S)
%Work through the integrated string character by character
c=S(i);
b=b+(c=='(')-(c==')'); %Keep track of how many layers deep of brackets we are in
if(c=='*'&&S(i+1)==r)||(b&&c==' ') %If a '*' sign preceeds a variable. Also deblank string.
c=''; %Delete this character
end
o=[o c]; %merge into new output string.
end
o=regexprep([char(8747) '(' t ')d' r ' = ' o 'C'],{'pi','exp\(1\)','exp','\^([^\(])',['1/' r]},{p,'e','e^','^($1)',[r '^(-1)']});
catch
e=1; %failed to integrate
end
if e||~isempty(strfind(o,'log'))
disp(['Cannot integrate ' t]) %bit of a hack - matlab can integrate 1/x, so if we get a log, we pretend it didn't work.
else
disp(o)% Display it.
end
Here are some examples of what it currently produces. As you can see, it's not quite right, but getting closer.
Inputs:
x
-f^(-2)
(1/7)x^(1/7) + 5
πx^e
(f+1)^(-1)
Outputs:
∫(x)dx = x^(2)/2 + C
∫(-f^(-2))df = f^(-1) + C
∫((1/7)x^(1/7) + 5)dx = x^(8/7)/8 + 5x + C
∫(πx^(e))dx = (πx^(e+1))/(e+1) + C
Cannot integrate (f+1)^(-1)
Mathematica 478 * 0.9 = 430.2
φ=(α=ToExpression;Π=StringReplace;σ="Cannot integrate "<>#1;Λ=DeleteDuplicates@StringCases[#1,RegularExpression["[a-df-zA-Z]+"]];μ=Length@Λ;If[μ>1,σ,If[μ<1,Λ="x",Λ=Λ[[1]]];Ψ=α@Π[#1,{"e"->" E ","π"->" π "}];Φ=α@Λ;Θ=α@Π[#1,{"e"->" 2 ","π"->" 2 "}];λ=Exponent[Θ,Φ,List];Θ=Simplify[Θ*Φ^Max@@Abs@λ];Θ=PowerExpand[Θ/.Φ->Φ^LCM@@Denominator@λ];If[Coefficient[Ψ,Φ,-1]==0&&PolynomialQ[Θ,Φ],"∫("<>#1<>")d"<>Λ<>" = "<>Π[ToString[Integrate[Ψ,Φ],InputForm],{"E"->"e","Pi"->"π"}]<>" + C",σ]])&
This creates a true function φ that takes one String as Input. (Does that count as complete program for Mathematica?)
The ungolfed version would be:
φ=(
σ="Cannot integrate "<>#1;
Λ=DeleteDuplicates@StringCases[#1,RegularExpression["[a-df-zA-Z]+"]];
If[Length@Λ>1,σ,
If[Length@Λ<1,Λ="x",Λ=Λ[[1]]];
Ψ=ToExpression@StringReplace[#1,{"e"->" E ","π"->" π "}];
Φ=ToExpression@Λ;
Θ=ToExpression@StringReplace[#1,{"e"->" 2 ","π"->" 2 "}];
λ=Exponent[Θ,Φ,List];
Θ=Simplify[Θ*Φ^Max@@Abs@λ];
Θ=PowerExpand[Θ/.Φ->Φ^LCM@@Denominator@λ];
If[Coefficient[Ψ,Φ,-1]==0&&PolynomialQ[Θ,Φ],
"∫("<>#1<>")d"<>Λ<>" = "<>StringReplace[ToString[Integrate[Ψ,Φ],InputForm],{"E"->"e","Pi"->"π"}]<>" + C",
σ
]
]
)&
Note that the Greek letters are necessary to be able to use all the other letters in the input.