> 
X := L10*cos(q5)  L16*(sin(q10)*(sin(q5)*sin(q8)  cos(q8)*(cos(q5)*cos(q6)*cos(q7)  cos(q5)*sin(q6)*sin(q7)))  cos(q10)*(sin(q9)*(cos(q8)*sin(q5) + sin(q8)*(cos(q5)*cos(q6)*cos(q7)  cos(q5)*sin(q6)*sin(q7))) + cos(q9)*(cos(q5)*cos(q6)*sin(q7) + cos(q5)*cos(q7)*sin(q6))))  d2*(cos(q10)*(sin(q5)*sin(q8)  cos(q8)*(cos(q5)*cos(q6)*cos(q7)  cos(q5)*sin(q6)*sin(q7))) + sin(q10)*(sin(q9)*(cos(q8)*sin(q5) + sin(q8)*(cos(q5)*cos(q6)*cos(q7)  cos(q5)*sin(q6)*sin(q7))) + cos(q9)*(cos(q5)*cos(q6)*sin(q7) + cos(q5)*cos(q7)*sin(q6)))) + L15*(sin(q9)*(cos(q8)*sin(q5) + sin(q8)*(cos(q5)*cos(q6)*cos(q7)  cos(q5)*sin(q6)*sin(q7))) + cos(q9)*(cos(q5)*cos(q6)*sin(q7) + cos(q5)*cos(q7)*sin(q6)))  L11*cos(q5)*sin(q6) + d1*cos(q5)*cos(q6)  L13*sin(q5)*sin(q8) + L14*cos(q9)*(cos(q8)*sin(q5) + sin(q8)*(cos(q5)*cos(q6)*cos(q7)  cos(q5)*sin(q6)*sin(q7))) + L13*cos(q8)*(cos(q5)*cos(q6)*cos(q7)  cos(q5)*sin(q6)*sin(q7))  L14*sin(q9)*(cos(q5)*cos(q6)*sin(q7) + cos(q5)*cos(q7)*sin(q6)) + L12*cos(q5)*cos(q6)*cos(q7)  L12*cos(q5)*sin(q6)*sin(q7):

> 
Y := L10*sin(q5)  L9 + L16*(sin(q10)*(cos(q5)*sin(q8)  cos(q8)*(sin(q5)*sin(q6)*sin(q7)  cos(q6)*cos(q7)*sin(q5)))  cos(q10)*(sin(q9)*(cos(q5)*cos(q8) + sin(q8)*(sin(q5)*sin(q6)*sin(q7)  cos(q6)*cos(q7)*sin(q5)))  cos(q9)*(cos(q6)*sin(q5)*sin(q7) + cos(q7)*sin(q5)*sin(q6)))) + d2*(cos(q10)*(cos(q5)*sin(q8)  cos(q8)*(sin(q5)*sin(q6)*sin(q7)  cos(q6)*cos(q7)*sin(q5))) + sin(q10)*(sin(q9)*(cos(q5)*cos(q8) + sin(q8)*(sin(q5)*sin(q6)*sin(q7)  cos(q6)*cos(q7)*sin(q5)))  cos(q9)*(cos(q6)*sin(q5)*sin(q7) + cos(q7)*sin(q5)*sin(q6))))  L15*(sin(q9)*(cos(q5)*cos(q8) + sin(q8)*(sin(q5)*sin(q6)*sin(q7)  cos(q6)*cos(q7)*sin(q5)))  cos(q9)*(cos(q6)*sin(q5)*sin(q7) + cos(q7)*sin(q5)*sin(q6))) + L13*cos(q5)*sin(q8)  L11*sin(q5)*sin(q6) + d1*cos(q6)*sin(q5)  L14*cos(q9)*(cos(q5)*cos(q8) + sin(q8)*(sin(q5)*sin(q6)*sin(q7)  cos(q6)*cos(q7)*sin(q5)))  L13*cos(q8)*(sin(q5)*sin(q6)*sin(q7)  cos(q6)*cos(q7)*sin(q5))  L14*sin(q9)*(cos(q6)*sin(q5)*sin(q7) + cos(q7)*sin(q5)*sin(q6)) + L12*cos(q6)*cos(q7)*sin(q5)  L12*sin(q5)*sin(q6)*sin(q7):

> 
Z := L15*(cos(q9)*(cos(q6)*cos(q7)  sin(q6)*sin(q7))  sin(q8)*sin(q9)*(cos(q6)*sin(q7) + cos(q7)*sin(q6)))  L11*cos(q6)  L8  d1*sin(q6) + L16*(cos(q10)*(cos(q9)*(cos(q6)*cos(q7)  sin(q6)*sin(q7))  sin(q8)*sin(q9)*(cos(q6)*sin(q7) + cos(q7)*sin(q6)))  cos(q8)*sin(q10)*(cos(q6)*sin(q7) + cos(q7)*sin(q6)))  d2*(sin(q10)*(cos(q9)*(cos(q6)*cos(q7)  sin(q6)*sin(q7))  sin(q8)*sin(q9)*(cos(q6)*sin(q7) + cos(q7)*sin(q6))) + cos(q8)*cos(q10)*(cos(q6)*sin(q7) + cos(q7)*sin(q6)))  L13*cos(q8)*(cos(q6)*sin(q7) + cos(q7)*sin(q6))  L14*sin(q9)*(cos(q6)*cos(q7)  sin(q6)*sin(q7))  L12*cos(q6)*sin(q7)  L12*cos(q7)*sin(q6)  L14*cos(q9)*sin(q8)*(cos(q6)*sin(q7) + cos(q7)*sin(q6)): length(X), length(Y), length(Z);

1817, 1822, 1197
> 
unknwns := [indets([X,Y,Z],And(name,Non(constant)))[]];

unknwns := [L10, L11, L12, L13, L14, L15, L16, L8, L9, d1, d2, q10, q5, q6, q7, q8, q9]
> 
f := unapply([X,Y,Z],[indets([X,Y,Z],And(name,Non(constant)))[]]):

> 
fopt := codegen[optimize](f,tryhard);

fopt := proc(L10, L11, L12, L13, L14, L15, L16, L8, L9, d1, d2, q10, q5, q6, q7, q8, q9) local result, t1, t10, t11, t12, t14, t15, t16, t17, t18, t19, t2, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t3, t30, t5, t6, t7, t8, t9; t18 := sin(q7); t19 := sin(q6); t23 := cos(q7); t24 := cos(q6); t12 := t18*t24 + t19*t23; t17 := sin(q8); t30 := t12*t17; t22 := cos(q8); t29 := t12*t22; t25 := cos(q5); t20 := sin(q5); t27 := t18*t19  t23*t24; t8 := t27*t20; t6 := t17*t25  t22*t8; t11 := t27*t25; t28 := t11*t22  t17*t20; t26 := L11*t19  L12*t27 + d1*t24 + L10; t21 := cos(q9); t16 := sin(q9); t15 := cos(q10); t14 := sin(q10); t10 := t12*t25; t9 := t12*t20; t7 := t17*t8 + t22*t25; t5 := t11*t17 + t20*t22; t3 := t16*t30  t21*t27; t2 := t16*t7  t21*t9; t1 := t10*t21 + t16*t5; result := [L16*(t1*t15  t14*t28)  d2*(t1*t14  t15*t28) + L15*t1 + (t10*t16 + t21*t5)*L14 + t28*L13 + t26*t25, L9 + L16*(t14*t6  t15*t2) + d2*(t14*t2 + t15*t6)  L15*t2 + (t16*t9  t21*t7)*L14 + t6*L13 + t26*t20, L15*t3  L11*t24  L8  d1*t19 + L16*(t14*t29 + t15*t3)  d2*(t14*t3 + t15*t29)  L13*t29 + (t16*t27  t21*t30)*L14  t12*L12]; end proc;
> 
A,B,C := simplify(X,size), simplify(Y,size), simplify(Z,size): length(B), length(B), length(C);

996, 996, 787
> 
g := unapply([A,B,C],[indets([A,B,C],And(name,Non(constant)))[]]): gopt := codegen[optimize](g,tryhard);

gopt := proc(L10, L11, L12, L13, L14, L15, L16, L8, L9, d1, d2, q10, q5, q6, q7, q8, q9) local result, t31, t32, t34, t36, t37, t39, t40, t41, t42, t43, t44, t45, t46, t47, t48, t49, t50, t52, t58, t61, t63; t39 := sin(q10); t40 := cos(q10); t37 := L16*t40  d2*t39 + L15; t41 := sin(q9); t46 := cos(q9); t34 := L14*t46 + t37*t41; t36 := L16*t39 + d2*t40 + L13; t42 := sin(q8); t47 := cos(q8); t32 := t34*t42  t36*t47  L12; t43 := sin(q7); t48 := cos(q7); t52 := L14*t41  t37*t46; t63 := t32*t48  t43*t52 + d1; t61 := t32*t43  t48*t52  L11; t58 := t34*t47 + t36*t42; t50 := cos(q5); t49 := cos(q6); t45 := sin(q5); t44 := sin(q6); t31 := t44*t61 + t49*t63 + L10; result := [t31*t50  t58*t45, t31*t45 + t58*t50  L9, t44*t63 + t49*t61  L8 ]; end proc;
93 additions + 229 multiplications + 289 functions
28 storage + 29 assignments + 12 functions + 65 multiplications + 40 additions
119 multiplications + 93 additions + 119 functions
21 storage + 22 assignments + 12 functions + 20 additions + 24 multiplications
> 
# check fopt simplify( fopt(unknwns[])  [X, Y, Z] );

[0, 0, 0]
> 
# check gopt simplify( gopt(unknwns[])  [X, Y, Z] );

[0, 0, 0]
> 
CodeGeneration[Matlab](gopt);

function goptreturn = gopt(L10, L11, L12, L13, L14, L15, L16, L8, L9, d1, d2, q10, q5, q6, q7, q8, q9)
t39 = sin(q10); t40 = cos(q10); t37 = L16 * t40  d2 * t39 + L15; t41 = sin(q9); t46 = cos(q9); t34 = L14 * t46 + t37 * t41; t36 = L16 * t39 + d2 * t40 + L13; t42 = sin(q8); t47 = cos(q8); t32 = t34 * t42  t36 * t47  L12; t43 = sin(q7); t48 = cos(q7); t52 = L14 * t41  t37 * t46; t63 = t32 * t48  t43 * t52 + d1; t61 = t32 * t43  t48 * t52  L11; t58 = t34 * t47 + t36 * t42; t50 = cos(q5); t49 = cos(q6); t45 = sin(q5); t44 = sin(q6); t31 = t44 * t61 + t49 * t63 + L10; result = [t31 * t50  t58 * t45 t31 * t45 + t58 * t50  L9 t44 * t63 + t49 * t61  L8]; goptreturn = result;
