We now do a demo where we optimize a function denoted sHWU in its variables W and U. We do this by setting the gradients of the sHWU in W, U to 0 and solving for W and U.
BEWARE YOU CAN NOT WORK THROUGH THIS DEMO YOURSELF UNLESS YOU HAVE INSTALLED OUR SYSTEMS PACKAGE.
In[2]:= <<NCAlgebra.m In[3]:= <<SYSDefIA.m??Loads in NCAlgebra and the function 'sHWU' and a bunch of formulas (which this demo does not need). Ignore the printout.
In[4]:= sHWU
Out[4]= -tp[W] ** W + (tp[U] ** tp[D12[x]] + tp[C1[x]]) **
(C1[x] + D12[x] ** U) + ((tp[U] ** tp[B2[x]] + tp[W] ** tp[B1[x]] +
tp[A[x]]) ** GEx[x, z] +
tp[GEx[x, z]] ** (A[x] + B1[x] ** W + B2[x] ** U))/2 +
(tp[GEz[x, z]] ** (b[z] ** (C2[x] + D21[x] ** W) + a[z]) +
((tp[W] ** tp[D21[x]] + tp[C2[x]]) ** tp[b[z]] + tp[a[z]]) **
GEz[x, z])/2
In[5]:= DirectionalD[%,W,h]
Use directional derivative to calculate the gradient of sHWU in W(safer than Grad). Next we shall set to 0 and solve for W.
Out[5]= -(tp[W] ** h + tp[h] ** W) +
(tp[h] ** tp[B1[x]] ** GEx[x, z] + tp[GEx[x, z]] ** B1[x] ** h)/2 +
(tp[h] ** tp[D21[x]] ** tp[b[z]] ** GEz[x, z] +
tp[GEz[x, z]] ** b[z] ** D21[x] ** h)/2
In[6]:= NCCSym[NCE[%],h]
This is an alias for NCollectSymmetric. It collects on h and tp[h] simultaneously.
Out[6]= (tp[GEx[x, z]] ** B1[x]/2 + tp[GEz[x, z]] ** b[z] ** D21[x]/2 -
tp[W]) ** h - tp[h] ** (W - tp[B1[x]] ** GEx[x, z]/2 -
tp[D21[x]] ** tp[b[z]] ** GEz[x, z]/2)
In[7]:= Substitute[ %, tp[h] -> 0]
Need to keep only half of this symmetric expression
when setting expression equal to 0.
Out[7]= (tp[GEx[x, z]] ** B1[x]/2 + tp[GEz[x, z]] ** b[z] ** D21[x]/2 -
tp[W]) ** h
In[8]:= Substitute[%, h -> 1]
Set h to 1 to pull off the operator (gradient).
Out[8]= tp[GEx[x, z]] ** B1[x]/2 + tp[GEz[x, z]] ** b[z] ** D21[x]/2 - tp[W]
In[9]:= tp[%]
Out[9]= -W + tp[B1[x]] ** GEx[x, z]/2 + tp[D21[x]] ** tp[b[z]] ** GEz[x, z]/2
In[10]:= NCSolve[%9==0,W]
NCCollect::notsum: Expression is not a sum of terms.
Out[10]= {W -> tp[B1[x]] ** GEx[x, z]/2 +
tp[D21[x]] ** tp[b[z]] ** GEz[x, z]/2}
In[11]:= sHU = NCExpand[Sub[sHWU, ruCRW]]
Out[11]= tp[A[x]] ** GEx[x, z]/2 + tp[C1[x]] ** C1[x] +
tp[GEx[x, z]] ** A[x]/2 + tp[GEz[x, z]] ** a[z]/2 +
tp[a[z]] ** GEz[x, z]/2 + tp[U] ** tp[B2[x]] ** GEx[x, z]/2 +
tp[U] ** tp[D12[x]] ** C1[x] + tp[C1[x]] ** D12[x] ** U +
tp[C2[x]] ** tp[b[z]] ** GEz[x, z]/2 +
tp[GEx[x, z]] ** B2[x] ** U/2 +
tp[GEz[x, z]] ** b[z] ** C2[x]/2 +
tp[U] ** tp[D12[x]] ** D12[x] ** U +
tp[GEx[x, z]] ** B1[x] ** tp[B1[x]] ** GEx[x, z]/4 +
tp[GEx[x, z]] ** B1[x] ** tp[D21[x]] ** tp[b[z]] ** GEz[x, z]/4 +
tp[GEz[x, z]] ** b[z] ** D21[x] ** tp[B1[x]] ** GEx[x, z]/4 +
tp[GEz[x, z]] ** b[z] ** D21[x] ** tp[D21[x]] **
tp[b[z]] ** GEz[x, z]/4
The W problem is solved; we have found the W which optimizes sHWU
and we substituted it back into sHWU to get the matrix valued optimal
sHU.
In[12]:= DirD[%,U,h]DirD is an alias for DirectionalD. Now we want critical U, using the same approach.
Out[12]= tp[h] ** tp[B2[x]] ** GEx[x, z]/2 + tp[h] ** tp[D12[x]] ** C1[x] +
tp[C1[x]] ** D12[x] ** h + tp[GEx[x, z]] ** B2[x] ** h/2 +
tp[U] ** tp[D12[x]] ** D12[x] ** h + tp[h] ** tp[D12[x]] ** D12[x] ** U
In[13]:= NCCSym[%,h]
Out[13]= (tp[C1[x]] ** D12[x] + tp[GEx[x, z]] ** B2[x]/2 +
tp[U] ** tp[D12[x]] ** D12[x]) ** h +
tp[h] ** (tp[B2[x]] ** GEx[x, z]/2 + tp[D12[x]] ** C1[x] +
tp[D12[x]] ** D12[x] ** U)
In[14]:= %[[1]]+tp[%[[2]]]
This time use a trick to keep the whole expression, first part + transpose[second].
Out[14]= 2*(tp[C1[x]] ** D12[x] + tp[GEx[x, z]] ** B2[x]/2 +
tp[U] ** tp[D12[x]] ** D12[x]) ** h
In[15]:= NCE[%]
Out[15]= 2*tp[C1[x]] ** D12[x] ** h + tp[GEx[x, z]] ** B2[x] ** h +
2*tp[U] ** tp[D12[x]] ** D12[x] ** h
In[16]:= Substitute[%, h -> 1]
Above NCE is an alias for NCExpand.
Again setting
to 1 pulls off the operator.
Out[16]= 2*tp[C1[x]] ** D12[x] + tp[GEx[x, z]] ** B2[x] + 2*tp[U] ** tp[D12[x]] ** D12[x] %%%%%%%%%%%%%%%%%%%%%% BEGIN CHANGES All is WRONG BELOW ?? We do not want this substite rue In[17]:= rue KILL ??We shorten the expression with a change of notation before setting to 0.
Out[17]= {tp[D12[x_]] ** D12[x_] :> e1[x],
D21[x_] ** tp[D21[x_]] :> e2[x]}
In[18]:= Sub[%%,rue] KILL??
Out[18]= 2*e1[x] ** U + tp[B2[x]] ** GEx[x, z] +
2*tp[D12[x]] ** C1[x]
In[19]:= ruCritU=NCSolve[%==0,U]
NCCollect::notsum: Expression is not a sum of terms.
Out[19]= {U -> -inv[e1[x]] ** tp[B2[x]] ** GEx[x, z]/2 -
inv[e1[x]] ** tp[D12[x]] ** C1[x]}
The U problem is solved; we have found the U which optimizes sHWU.
We substituted it back into sHU to get the matrix valued optimal
sHU.
In[20]:= sH = Substitute[ sHU, ruCritU]
Here, at last, is the answer:
Out[20]= tp[A[x]] ** GEx[x, z]/2 + tp[C1[x]] ** C1[x] +
tp[GEx[x, z]] ** A[x]/2 + tp[GEz[x, z]] ** a[z]/2 +
tp[a[z]] ** GEz[x, z]/2 + (-tp[C1[x]] ** D12[x] ** inv[e1[x]] -
tp[GEx[x, z]] ** B2[x] ** inv[e1[x]]/2) **
tp[B2[x]] ** GEx[x, z]/2 +
(-tp[C1[x]] ** D12[x] ** inv[e1[x]] -
tp[GEx[x, z]] ** B2[x] ** inv[e1[x]]/2) ** tp[D12[x]] ** C1[x] +
tp[C1[x]] ** D12[x] ** (-inv[e1[x]] ** tp[B2[x]] ** GEx[x, z]/2 -
inv[e1[x]] ** tp[D12[x]] ** C1[x]) +
tp[C2[x]] ** tp[b[z]] ** GEz[x, z]/2 +
tp[GEx[x, z]] ** B2[x] ** (-inv[e1[x]] ** tp[B2[x]] ** GEx[x, z]/2 -
inv[e1[x]] ** tp[D12[x]] ** C1[x])/2 +
tp[GEz[x, z]] ** b[z] ** C2[x]/2 +
(-tp[C1[x]] ** D12[x] ** inv[e1[x]] -
tp[GEx[x, z]] ** B2[x] ** inv[e1[x]]/2) ** tp[D12[x]] ** D12[x] **
(-inv[e1[x]] ** tp[B2[x]] ** GEx[x, z]/2 -
inv[e1[x]] ** tp[D12[x]] ** C1[x]) +
tp[GEx[x, z]] ** B1[x] ** tp[B1[x]] ** GEx[x, z]/4 +
tp[GEx[x, z]] ** B1[x] ** tp[D21[x]] ** tp[b[z]] ** GEz[x, z]/4 +
tp[GEz[x, z]] ** b[z] ** D21[x] ** tp[B1[x]] ** GEx[x, z]/4 +
tp[GEz[x, z]] ** b[z] ** D21[x] ** tp[D21[x]] **
tp[b[z]] ** GEz[x, z]/4
END CHANGES