I was pleasantly surprised about ability of GPT-4 to generate Mathematica code. Larger pieces can contain subtle bugs, but for simple problems it’s quite impressive, examples below. Curious to see other examples of how common Mathematica workflows can be improved using GPT-4. For instance I asked the following: “Can you write code in Mathematica to visualize the trajectory of gradient descent when minimizing the function f(x1,x2,x3)=x1^2+x2^2/2+x3^2/3 and starting with {x1,x2,x3}={1,1,1}”. And it generated the following functioning code:
f[{x1_,x2_,x3_}]:=x1^2+x2^2/2+x3^2/3gradF[{x1_,x2_,x3_}]:={2*x1,x2,2*x3/3}gradientDescent[f_,gradF_,x0_,stepSize_,numIterations_]:=Module[{x=x0,trajectory},trajectory=Table[x=x-stepSize*gradF[x],{numIterations}];Prepend[trajectory,x0]]; initialPoint={1,1,1};stepSize=0.1;numIterations=50;trajectory=gradientDescent[f,gradF,initialPoint,stepSize,numIterations];Show[ListPointPlot3D[trajectory,PlotStyleDirective[PointSize[Large],Red]],Graphics3D[{Thick,Line[trajectory]}],PlotRangeAll,AxesLabel{"x1","x2","x3"},LabelStyleDirective[Bold,Large],BoxRatios{1,1,1},SphericalRegionTrue,ImageSizeLarge]
Out[]=
Then I asked to do a 2D plot, by projecting to plane perpendicular to {1,1,1}, and give all code in a self-contained runnable block, and it gave me this:
In[]:=
(*Definethefunctionanditsgradient*)f[{x1_,x2_,x3_}]:=x1^2+x2^2/2+x3^2/3gradF[{x1_,x2_,x3_}]:={2*x1,x2,2*x3/3}(*GradientDescent*)gradientDescent[f_,gradF_,x0_,stepSize_,numIterations_]:=Module[{x=x0,trajectory},trajectory=Table[x=x-stepSize*gradF[x],{numIterations}];Prepend[trajectory,x0]](*Projectionfunction*)projectPointOntoPlane[P_,N_,Q_:{0,0,0}]:=P-(P-Q).N*N/N.N(*Gradientdescentparameters*)initialPoint={1,1,1};stepSize=0.1;numIterations=50;(*Rungradientdescent*)trajectory=gradientDescent[f,gradF,initialPoint,stepSize,numIterations];(*Projectthetrajectoryontotheplaneperpendicularto{1,1,1}*)normalVector={1,1,1};projectedTrajectory=projectPointOntoPlane[#,normalVector]&/@trajectory;(*Findtwoorthogonalbasisvectorsfortheplane*)basis1=Orthogonalize[{normalVector,{1,0,0}}][[2]];basis2=Cross[normalVector,basis1];(*Expresstheprojectedpointsintermsofthenewbasisvectors*)projectedTrajectory2D={#1.basis1,#1.basis2}&/@projectedTrajectory;(*Visualizetheprojectedtrajectoryin2D*)Show[ListPlot[projectedTrajectory2D,JoinedTrue,PlotMarkers{Automatic,Medium},AxesLabel{"Basis1","Basis2"},LabelStyleDirective[Bold,Large],ImageSizeLarge],PlotLabel"Gradient Descent Trajectory on Plane Perpendicular to {1, 1, 1}"]
Out[]=
During evaluation of In[1]:=