piecewise.mw

Computing derivatives of functions defined with "piecewise" in Maple 

 

Clara Masse, John Masse and François Ollivier 

 

Using a naive procedure 

> F := proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else 1 end if end proc
 

proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else 1 end if end proc (1)
 

Operators "diff" and "D" do not act exactly in the same way. 

> diff(F(x), x)
 

`+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) (2)
 

> D(F)
 

proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else 0 end if end proc (3)
 

A less naive procedure 

> F := subs(T = simplify(`/`(`*`(convert(taylor(`+`(1, `-`(cos(x))), x = 0, 12), polynom)), `*`(x))), proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else T end if end proc)
F := subs(T = simplify(`/`(`*`(convert(taylor(`+`(1, `-`(cos(x))), x = 0, 12), polynom)), `*`(x))), proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else T end if end proc)
 

proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151200, ...
proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151200, ...
proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151200, ...
proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151200, ...
proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151200, ...
proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151200, ...
proc (x) if `<>`(x, 0) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151200, ...
(4)
 

> D(F)
 

proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else `+`(`*`(`/`(1, 3628800), `*`(`^`(x, 8))), `-`(`*`(`/`(1, 40320), `*`(`^`(x, 6)))), `*...
proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else `+`(`*`(`/`(1, 3628800), `*`(`^`(x, 8))), `-`(`*`(`/`(1, 40320), `*`(`^`(x, 6)))), `*...
proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else `+`(`*`(`/`(1, 3628800), `*`(`^`(x, 8))), `-`(`*`(`/`(1, 40320), `*`(`^`(x, 6)))), `*...
proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else `+`(`*`(`/`(1, 3628800), `*`(`^`(x, 8))), `-`(`*`(`/`(1, 40320), `*`(`^`(x, 6)))), `*...
proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else `+`(`*`(`/`(1, 3628800), `*`(`^`(x, 8))), `-`(`*`(`/`(1, 40320), `*`(`^`(x, 6)))), `*...
proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else `+`(`*`(`/`(1, 3628800), `*`(`^`(x, 8))), `-`(`*`(`/`(1, 40320), `*`(`^`(x, 6)))), `*...
proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else `+`(`*`(`/`(1, 3628800), `*`(`^`(x, 8))), `-`(`*`(`/`(1, 40320), `*`(`^`(x, 6)))), `*...
proc (x) if `<>`(x, 0) then `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2))))) else `+`(`*`(`/`(1, 3628800), `*`(`^`(x, 8))), `-`(`*`(`/`(1, 40320), `*`(`^`(x, 6)))), `*...
(5)
 

Translation in Fortran 

> with(CodeGeneration); -1
 

> Fortran(F)
 

     doubleprecision function F (x)
 

       integer x
       if (x .ne. 0) then
         F = (0.1D1 - cos(dble(x))) / dble(x)
         return
       else
         F = dble(x * (x ** 8 - 90 * x ** 6 + 5040 * x ** 4 - 151200 *
    #x ** 2 + 1814400)) / 0.3628800D7
         return
       end if
     end
 

Using "piecewise" 

> G := proc (x) options operator, arrow; piecewise(`<>`(x, 0), `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)), 0) end proc
 

proc (x) options operator, arrow; piecewise(`<>`(x, 0), `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)), 0) end proc (6)
 

> diff(G(x), x)
 

piecewise(x = 0, `/`(1, 2), `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2)))))) (7)
 

> D[1](G)
 

proc (x) options operator, arrow; piecewise(x = 0, `/`(1, 2), `+`(`/`(`*`(sin(x)), `*`(x)), `-`(`/`(`*`(`+`(1, `-`(cos(x)))), `*`(`^`(x, 2)))))) end proc (8)
 

> D[1, 1, 1](G)
 

proc (x) options operator, arrow; piecewise(x = 0, -`/`(1, 4), `+`(`-`(`/`(`*`(sin(x)), `*`(x))), `-`(`/`(`*`(3, `*`(cos(x))), `*`(`^`(x, 2)))), `/`(`*`(6, `*`(sin(x))), `*`(`^`(x, 3))), `-`(`/`(`*`(6... (9)
 

> Fortran(G)
 

 

Warning, procedure/module options ignored
     doubleprecision function G (x)
       integer x
       if (x .ne. 0) then
         G = (0.1D1 - cos(dble(x))) / dble(x)
         return
       else
         G = 0.0D0
         return
       end if
     end
 

 

> H := subs(T = simplify(`/`(`*`(convert(taylor(`+`(1, `-`(cos(x))), x = 0, 12), polynom)), `*`(x))), proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else T end if end proc)
H := subs(T = simplify(`/`(`*`(convert(taylor(`+`(1, `-`(cos(x))), x = 0, 12), polynom)), `*`(x))), proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else T end if end proc)
 

proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151...
proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151...
proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151...
proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151...
proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151...
proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151...
proc (x) if `<`(.1, abs(x)) then `/`(`*`(`+`(1, `-`(cos(x)))), `*`(x)) else `+`(`*`(`/`(1, 3628800), `*`(x, `*`(`+`(`*`(`^`(x, 8)), `-`(`*`(90, `*`(`^`(x, 6)))), `*`(5040, `*`(`^`(x, 4))), `-`(`*`(151...
(10)
 

> Fortran(H)
 

     doubleprecision function H (x)
       integer x
 

       if (0.1D0 .lt. dble(abs(x))) then
         H = (0.1D1 - cos(dble(x))) / dble(x)
         return
       else
         H = dble(x * (x ** 8 - 90 * x ** 6 + 5040 * x ** 4 - 151200 *
    #x ** 2 + 1814400)) / 0.3628800D7
         return
       end if
     end
 

>