OptimiseAlgebra.m 862 B

1234567891011121314151617181920212223242526272829
  1. function [SymExpOut,SubExpArray] = OptimiseAlgebra(SymExpIn,SubExpName)
  2. % Loop through symbolic expression, identifying repeated expressions and
  3. % bringing them out as shared expression or sub expressions
  4. % do this until no further repeated expressions found
  5. % This can significantly reduce computations
  6. syms SubExpIn SubExpArray ;
  7. SubExpArray(1,1) = 'invalid';
  8. index = 0;
  9. f_complete = 0;
  10. while f_complete==0
  11. index = index + 1;
  12. SubExpIn = [SubExpName,'(',num2str(index),')'];
  13. SubExpInStore{index} = SubExpIn;
  14. [SymExpOut,SubExpOut]=subexpr(SymExpIn,SubExpIn);
  15. for k = 1:index
  16. if SubExpOut == SubExpInStore{k}
  17. f_complete = 1;
  18. end
  19. end
  20. if f_complete || index > 100
  21. SymExpOut = SymExpIn;
  22. else
  23. SubExpArray(index,1) = SubExpOut;
  24. SymExpIn = SymExpOut;
  25. end
  26. end