482482function mul! (res:: Taylor1{TaylorN{T}} , a:: Taylor1{TaylorN{T}} , b:: Taylor1{TaylorN{T}} ,
483483 ordT:: Int ) where {T<: NumberNotSeries }
484484 # Sanity
485- zero! (res, a, ordT)
485+ zero! (res, ordT)
486486 for k in 0 : ordT
487487 @inbounds for ordQ in eachindex (a[ordT])
488488 mul! (res[ordT], a[k], b[ordT- k], ordQ)
@@ -491,6 +491,27 @@ function mul!(res::Taylor1{TaylorN{T}}, a::Taylor1{TaylorN{T}}, b::Taylor1{Taylo
491491 return nothing
492492end
493493
494+ @inline function mul! (res:: Taylor1{TaylorN{T}} , a:: NumberNotSeries ,
495+ b:: Taylor1{TaylorN{T}} , k:: Int ) where {T<: NumberNotSeries }
496+ for l in eachindex (b[k])
497+ for m in eachindex (b[k][l])
498+ res[k][l][m] = a* b[k][l][m]
499+ end
500+ end
501+ return nothing
502+ end
503+
504+ mul! (res:: Taylor1{TaylorN{T}} , a:: Taylor1{TaylorN{T}} ,
505+ b:: NumberNotSeries , k:: Int ) where {T<: NumberNotSeries } = mul! (res, b, a, k)
506+
507+ # in-place division (assumes equal order among TaylorNs)
508+ function mul! (c:: TaylorN , a:: TaylorN , b:: TaylorN )
509+ for k in eachindex (c)
510+ mul! (c, a, b, k)
511+ end
512+ end
513+
514+
494515
495516@doc doc"""
496517 mul!(c, a, b, k::Int) --> nothing
@@ -665,6 +686,27 @@ function /(a::Taylor1{TaylorN{T}}, b::Taylor1{TaylorN{T}}) where {T<:NumberNotSe
665686 return res
666687end
667688
689+ function / (a:: S , b:: Taylor1{TaylorN{T}} ) where {S<: NumberNotSeries , T<: NumberNotSeries }
690+ R = promote_type (TaylorN{S}, TaylorN{T})
691+ res = convert (Taylor1{R}, zero (b))
692+ iszero (a) && ! iszero (b) && return res
693+
694+ for ordT in eachindex (res)
695+ div! (res, a, b, ordT)
696+ end
697+ return res
698+ end
699+
700+ function / (a:: TaylorN{T} , b:: Taylor1{TaylorN{T}} ) where {T<: NumberNotSeries }
701+ res = zero (b)
702+ iszero (a) && ! iszero (b) && return res
703+
704+ aa = Taylor1 (a, b. order)
705+ for ordT in eachindex (res)
706+ div! (res, aa, b, ordT)
707+ end
708+ return res
709+ end
668710
669711@inline function divfactorization (a1:: Taylor1 , b1:: Taylor1 )
670712 # order of first factorized term; a1 and b1 assumed to be of the same order
@@ -731,23 +773,94 @@ end
731773 return nothing
732774end
733775
734- div! (v:: Taylor1 , b:: NumberNotSeries , a:: Taylor1 , k:: Int ) =
735- div! (v:: Taylor1 , Taylor1 (b, a. order), a, k)
776+ @inline function div! (c:: Taylor1{T} , a:: NumberNotSeries ,
777+ b:: Taylor1{T} , k:: Int ) where {T<: Number }
778+ iszero (a) && ! iszero (b) && zero! (c, k)
779+ # order and coefficient of first factorized term
780+ # In this case, since a[k]=0 for k>0, we can simplify to:
781+ # ordfact, cdivfact = 0, a/b[0]
782+ if k == 0
783+ @inbounds c[0 ] = a/ b[0 ]
784+ return nothing
785+ end
786+
787+ @inbounds c[k] = c[0 ] * b[k]
788+ @inbounds for i = 1 : k- 1
789+ c[k] += c[i] * b[k- i]
790+ end
791+ @inbounds c[k] = - c[k]/ b[0 ]
792+ return nothing
793+ end
794+
795+ # TODO : get rid of remaining allocations here.
796+ # This can either be achieved via pre-allocating auxiliary variables
797+ # with can be passed here as arguments, or adding allocation-free in-place
798+ # methods for operations such as `a += a*b` and `a = -a/b`, where a and b are
799+ # TaylorN variables. See #347 for further discussion.
800+ @inline function div! (c:: Taylor1{TaylorN{T}} , a:: NumberNotSeries ,
801+ b:: Taylor1{TaylorN{T}} , k:: Int ) where {T<: NumberNotSeries }
802+ iszero (a) && ! iszero (b) && zero! (c, k)
803+ # order and coefficient of first factorized term
804+ # In this case, since a[k]=0 for k>0, we can simplify to:
805+ # ordfact, cdivfact = 0, a/b[0]
806+ if k == 0
807+ @inbounds div! (c[0 ], a, b[0 ])
808+ return nothing
809+ end
810+
811+ @inbounds mul! (c[k], c[0 ], b[k])
812+ @inbounds for i = 1 : k- 1
813+ c[k] += c[i] * b[k- i]
814+ end
815+ @inbounds c[k] = - c[k]/ b[0 ]
816+ return nothing
817+ end
736818
737819# NOTE: Here `div!` *accumulates* the result of a / b in c[k] (k > 0)
738820@inline function div! (c:: TaylorN , a:: TaylorN , b:: TaylorN , k:: Int )
739821 if k== 0
740- @inbounds c[0 ] = a[ 0 ] / constant_term (b)
822+ @inbounds c[0 ][ 1 ] = constant_term (a) / constant_term (b)
741823 return nothing
742824 end
743825
744826 @inbounds for i = 0 : k- 1
745827 mul! (c[k], c[i], b[k- i])
746828 end
747- @inbounds c[k] = (a[k] - c[k]) / constant_term (b)
829+ @inbounds for i in eachindex (c[k])
830+ c[k][i] = (a[k][i] - c[k][i]) / constant_term (b)
831+ end
748832 return nothing
749833end
750834
835+ # NOTE: Here `div!` *accumulates* the result of a / b in c[k] (k > 0)
836+ @inline function div! (c:: TaylorN , a:: NumberNotSeries , b:: TaylorN , k:: Int )
837+ if k== 0
838+ @inbounds c[0 ][1 ] = a / constant_term (b)
839+ return nothing
840+ end
841+
842+ @inbounds for i = 0 : k- 1
843+ mul! (c[k], c[i], b[k- i])
844+ end
845+ @inbounds for i in eachindex (c[k])
846+ c[k][i] = ( - c[k][i] ) / constant_term (b)
847+ end
848+ return nothing
849+ end
850+
851+ # in-place division (assumes equal order among TaylorNs)
852+ function div! (c:: TaylorN , a:: TaylorN , b:: TaylorN )
853+ for k in eachindex (c)
854+ div! (c, a, b, k)
855+ end
856+ end
857+
858+ function div! (c:: TaylorN , a:: NumberNotSeries , b:: TaylorN )
859+ for k in eachindex (c)
860+ div! (c, a, b, k)
861+ end
862+ end
863+
751864# NOTE: Here `div!` *accumulates* the result of a / b in res[k] (k > 0)
752865@inline function div! (res:: Taylor1{TaylorN{T}} , a:: Taylor1{TaylorN{T}} ,
753866 b:: Taylor1{TaylorN{T}} , ordT:: Int ) where {T<: NumberNotSeriesN }
758871 @inbounds res[0 ] = cdivfact
759872 return nothing
760873 end
761- zero! (res, a, ordT)
874+ zero! (res, ordT)
762875 imin = max (0 , ordT+ ordfact- b. order)
763876 aux = TaylorN (zero (a[ordT][0 ][1 ]), a[ordT]. order)
764877 for k in imin: ordT- 1
783896 return nothing
784897end
785898
899+ @inline function div! (res:: Taylor1{TaylorN{T}} , a:: Taylor1{TaylorN{T}} ,
900+ b:: NumberNotSeries , k:: Int ) where {T<: NumberNotSeries }
901+ for l in eachindex (a[k])
902+ for m in eachindex (a[k][l])
903+ res[k][l][m] = a[k][l][m]/ b
904+ end
905+ end
906+ return nothing
907+ end
786908
787909
788910
0 commit comments