@@ -211,12 +211,15 @@ while(time <= 1)
211211end 
212212``` 
213213""" 
214- function  fractional_to_ordinary (eqs, variables, alphas, epsilon, T; initials =  0 , additional_eqs =  [], iv =  only (@independent_variables  t))
214+ function  fractional_to_ordinary (
215+         eqs, variables, alphas, epsilon, T;
216+         initials =  0 , additional_eqs =  [], iv =  only (@independent_variables  t), matrix= false 
217+ )
215218    D =  Differential (iv)
216219    i =  0 
217220    all_eqs =  Equation[]
218-     all_def =  Pair{Num, Int64} []
219- 
221+     all_def =  Pair[]
222+      
220223    function  fto_helper (sub_eq, sub_var, α; initial= 0 )
221224        alpha_0 =  α
222225
@@ -248,38 +251,61 @@ function fractional_to_ordinary(eqs, variables, alphas, epsilon, T; initials = 0
248251        end 
249252
250253        new_eqs =  Equation[]
251-         def =  Pair{Num, Int64} []
254+         def =  Pair[]
252255
253-         if  (α <  1 )  
254-             rhs =  initial
255-             for  index in  range (M, N- 1 ; step= 1 )
256-                 new_z =  Symbol (:ʐ , :_ , i)
257-                 i +=  1 
258-                 new_z =  ModelingToolkit. unwrap (only (@variables  $ new_z (iv)))
259-                 new_eq =  D (new_z) ~  sub_eq -  γ_i (index)* new_z
260-                 push! (new_eqs, new_eq)
261-                 push! (def, new_z=> 0 )
262-                 rhs +=  c_i (index)* new_z
256+         if  matrix
257+             new_z =  Symbol (:ʐ , :_ , i)
258+             i +=  1 
259+             γs =  diagm ([γ_i (index) for  index in  M: N- 1 ])
260+             cs =  [c_i (index) for  index in  M: N- 1 ]
261+ 
262+             if  (α <  1 )
263+                 new_z =  only (@variables  $ new_z (iv)[1 : N- M])
264+                 new_eq =  D (new_z) ~  - γs* new_z .+  sub_eq
265+                 rhs =  dot (cs, new_z) +  initial
266+                 push! (def, new_z=> zeros (N- M))
267+             else 
268+                 new_z =  only (@variables  $ new_z (iv)[1 : N- M, 1 : m])
269+                 new_eq =  D (new_z) ~  - γs* new_z +  hcat (fill (sub_eq, N- M, 1 ), collect (new_z[:, 1 : m- 1 ]* diagm (1 : m- 1 )))
270+                 rhs =  coeff* sum (cs[i]* new_z[i, m] for  i in  1 : N- M)
271+                 for  (index, value) in  enumerate (initial)
272+                     rhs +=  value *  iv^ (index -  1 ) /  gamma (index)
273+                 end 
274+                 push! (def, new_z=> zeros (N- M, m))
263275            end 
276+             push! (new_eqs, new_eq)
264277        else 
265-             rhs =  0 
266-             for  (index, value) in  enumerate (initial)
267-                 rhs +=  value *  iv^ (index -  1 ) /  gamma (index)
268-             end 
269-             for  index in  range (M, N- 1 ; step= 1 )
270-                 new_z =  Symbol (:ʐ , :_ , i)
271-                 i +=  1 
272-                 γ =  γ_i (index)
273-                 base =  sub_eq
274-                 for  k in  range (1 , m; step= 1 )
275-                     new_z =  Symbol (:ʐ , :_ , index- M, :_ , k)
278+             if  (α <  1 )  
279+                 rhs =  initial
280+                 for  index in  range (M, N- 1 ; step= 1 )
281+                     new_z =  Symbol (:ʐ , :_ , i)
282+                     i +=  1 
276283                    new_z =  ModelingToolkit. unwrap (only (@variables  $ new_z (iv)))
277-                     new_eq =  D (new_z) ~  base -  γ* new_z
278-                     base =  k *  new_z
284+                     new_eq =  D (new_z) ~  sub_eq -  γ_i (index)* new_z
279285                    push! (new_eqs, new_eq)
280286                    push! (def, new_z=> 0 )
287+                     rhs +=  c_i (index)* new_z
288+                 end 
289+             else 
290+                 rhs =  0 
291+                 for  (index, value) in  enumerate (initial)
292+                     rhs +=  value *  iv^ (index -  1 ) /  gamma (index)
293+                 end 
294+                 for  index in  range (M, N- 1 ; step= 1 )
295+                     new_z =  Symbol (:ʐ , :_ , i)
296+                     i +=  1 
297+                     γ =  γ_i (index)
298+                     base =  sub_eq
299+                     for  k in  range (1 , m; step= 1 )
300+                         new_z =  Symbol (:ʐ , :_ , index- M, :_ , k)
301+                         new_z =  ModelingToolkit. unwrap (only (@variables  $ new_z (iv)))
302+                         new_eq =  D (new_z) ~  base -  γ* new_z
303+                         base =  k *  new_z
304+                         push! (new_eqs, new_eq)
305+                         push! (def, new_z=> 0 )
306+                     end 
307+                     rhs +=  coeff* c_i (index)* new_z
281308                end 
282-                 rhs +=  coeff* c_i (index)* new_z
283309            end 
284310        end 
285311        push! (new_eqs, sub_var ~  rhs)
@@ -385,78 +411,6 @@ function linear_fractional_to_ordinary(degrees, coeffs, rhs, epsilon, T; initial
385411    return  mtkcompile (sys)
386412end 
387413
388- function  fto_matrix (eqs, variables, alphas, epsilon, T; initials =  0 , additional_eqs =  [], iv =  only (@independent_variables  t))
389-     D =  Differential (iv)
390-     i =  0 
391-     all_eqs =  Equation[]
392-     all_def =  Pair[]
393- 
394-     function  fto_helper (sub_eq, sub_var, α; initial= 0 )
395-         alpha_0 =  α
396- 
397-         if  (α >  1 )
398-             coeff =  1 / (α -  1 )
399-             m =  2 
400-             while  (α -  m >  0 )
401-                 coeff /=  α -  m
402-                 m +=  1 
403-             end 
404-             alpha_0 =  α -  m +  1 
405-         end 
406- 
407-         δ =  (gamma (alpha_0+ 1 ) *  epsilon)^ (1 / alpha_0)
408-         a =  pi / 2 * (1 - (1 - alpha_0)/ ((2 - alpha_0) *  log (epsilon^- 1 )))
409-         h =  2 * pi * a /  log (1  +  (2 / epsilon *  (cos (a))^ (alpha_0 -  1 )))
410- 
411-         x_sub =  (gamma (2 - alpha_0) *  epsilon)^ (1 / (1 - alpha_0))
412-         x_sup =  - log (gamma (1 - alpha_0) *  epsilon)
413-         M =  floor (Int, log (x_sub /  T) /  h)
414-         N =  ceil (Int, log (x_sup /  δ) /  h)
415- 
416-         function  c_i (index)
417-             h *  sin (pi  *  alpha_0) /  pi  *  exp ((1 - alpha_0)* h* index)
418-         end 
419- 
420-         function  γ_i (index)
421-             exp (h *  index)
422-         end 
423- 
424-         new_eqs =  Equation[]
425-         def =  Pair[]
426-         new_z =  Symbol (:ʐ , :_ , i)
427-         i +=  1 
428-         γs =  diagm ([γ_i (index) for  index in  M: N- 1 ])
429-         cs =  [c_i (index) for  index in  M: N- 1 ]
430- 
431-         if  (α <  1 )
432-             new_z =  only (@variables  $ new_z (iv)[1 : N- M])
433-             new_eq =  D (new_z) ~  - γs* new_z .+  sub_eq
434-             rhs =  dot (cs, new_z) +  initial
435-             push! (def, new_z=> zeros (N- M))
436-         else 
437-             new_z =  only (@variables  $ new_z (iv)[1 : N- M, 1 : m])
438-             new_eq =  D (new_z) ~  - γs* new_z +  hcat (fill (sub_eq, N- M, 1 ), collect (new_z[:, 1 : m- 1 ]* diagm (1 : m- 1 )))
439-             rhs =  coeff* sum (cs[i]* new_z[i, m] for  i in  1 : N- M)
440-             for  (index, value) in  enumerate (initial)
441-                 rhs +=  value *  iv^ (index -  1 ) /  gamma (index)
442-             end 
443-             push! (def, new_z=> zeros (N- M, m))
444-         end 
445-         push! (new_eqs, new_eq)
446-         push! (new_eqs, sub_var ~  rhs)
447-         return  (new_eqs, def)
448-     end 
449- 
450-     for  (eq, cur_var, alpha, init) in  zip (eqs, variables, alphas, initials)
451-         (new_eqs, def) =  fto_helper (eq, cur_var, alpha; initial= init)
452-         append! (all_eqs, new_eqs)
453-         append! (all_def, def)
454-     end 
455-     append! (all_eqs, additional_eqs)
456-     @named  sys =  System (all_eqs, iv; defaults= all_def)
457-     return  mtkcompile (sys)
458- end 
459- 
460414""" 
461415    change_independent_variable( 
462416        sys::System, iv, eqs = []; 
0 commit comments