% Copyright (C)  2020 Alain Matthes
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% This work has the LPPL maintenance status `maintained'.
% The Current Maintainer of this work is Alain Matthes

% Objet :  fonctions numériques en 2D with gnuplot 
% constants from tkz-base 
% \tkz@init@xstep
% \tkz@init@ystep
\typeout{2020/05/01 1.4c function tkz-fct}
%<--------------------------------------------------------------------------->
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tkz-fct}[2020/05/01 1.4c function ] 
\newcount\tkz@tkzf@fct
\edef\tkzTWOPTCode{\the\catcode`\:} 
\tkz@tkzf@fct=0 % 
\def\tkzfctloaded{} 
%<--------------------------------------------------------------------------->
%                          Initialisation
%<--------------------------------------------------------------------------->
\RequirePackage{tkz-base,fp}
% % Code from Christian Tellechea 2009 % modified by AM
% modified \FP@pow from fp.sty
\def\FP@pow#1#2#3{%
 \FP@beginmessage{POW}%
 {\def\FP@beginmessage##1{}%
 \def\FP@endmessage##1{}%
 \FPifzero{#2}%
     \expandafter\@firstoftwo
 \else
     \expandafter\@secondoftwo
 \fi
 {\FP@pow@zero{#3}}%
 {\FPifint{#3}%
     \expandafter\@firstoftwo
 \else
    \expandafter\@secondoftwo
 \fi  
{\FPifneg{#2}%
 \FPneg\FP@tmpd{#2}%
 \FPln\FP@tmpd\FP@tmpd
 \FPmul\FP@tmpd\FP@tmpd{#3}%
 \FPexp\FP@tmpd\FP@tmpd
 \FPtrunc\FP@tmp{#3}0%
 \ifodd\FP@tmp
     \FPneg\FP@tmp\FP@tmpd
 \else
     \let\FP@tmp\FP@tmpd
 \fi
\else
 \FPln\FP@tmpd{#2}%
 \FPmul\FP@tmpd\FP@tmpd{#3}%
 \FPexp\FP@tmp\FP@tmpd
\fi
}% 
{\FPln\FP@tmpd{#2}%
 \FPmul\FP@tmpd\FP@tmpd{#3}%
 \FPexp\FP@tmp\FP@tmpd}%
}%
\global\let\FP@tmp\FP@tmp}%
\FP@endmessage{}%
\let#1\FP@tmp}
%<--------------------------------------------------------------------------->
%                               Styles 
%<--------------------------------------------------------------------------->
\def\tkzfctset#1{\pgfkeys{/tkztan/.cd,#1}}

\tkzfctset{tan style/.style={->,>=latex}}                %<--------------------------------------------------------------------------->
%                               tkzFct 
%<--------------------------------------------------------------------------->
\newif\iftkz@draw 
\newif\iftkz@fp
\def\tkz@dmin{-5}\def\tkz@dmax{5} 
\global\let\tkz@tmp@xa\tkz@init@xmin% modif  2016
\global\let\tkz@tmp@xb\tkz@init@xmax% modif  2016
\global\let\tkz@tmp@ya\tkz@init@ymin% modif  2016
\global\let\tkz@tmp@yb\tkz@init@ymax% modif  2016 
\catcode`\:=12
\pgfkeys{/@tkzfct/.cd,
      domain/.code args     = {#1:#2} {\FPeval\tkz@min{(#1)}
                                       \FPeval\tkz@max{(#2)}},
      domain                = \tkz@tmp@xa:\tkz@tmp@xb,
      samples/.store in     = \tkz@fct@samples, 
      id/.store in          = \tkz@fct@id,
      fp/.is          if    = tkz@fp,
      fp/.default           = false,
      samples               = 200,
      fp                    = true,
      id                    = tkzfct,  
     /@tkzfct/.search also  = {/tikz},
}%

\def\tkzFct{\pgfutil@ifnextchar[{\tkzActivOff\tkz@fct}{\tkz@fct[]}} 
\def\tkz@fct[#1]#2{%
\pgfqkeys{/@tkzfct}{#1}%
\iftkz@init@NO%
  \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
  \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
\else
  \FPset{\tkz@x@delta}{0}%
  \FPset{\tkz@y@delta}{0}%
\fi% 
% stockage
\advance\tkz@tkzf@fct by  1 %
\FPdiv\tkz@ba{\tkz@min}{\tkz@init@xstep}%
\FPdiv\tkz@bb{\tkz@max}{\tkz@init@xstep}%
\def\x{(x*\tkz@init@xstep)}%
\expandafter\edef\csname tkzFctgnu\@alph\tkz@tkzf@fct \endcsname{#2}%
\expandafter\edef\csname tkzFctgnuLast\endcsname{#2}     
%%%%%%%
\begin{scope}
  \clip (\tkz@xa,\tkz@ya) rectangle (\tkz@xb,\tkz@yb);
  % \draw[#1,  xshift  = -\tkz@x@delta cm,yshift  = -\tkz@y@delta cm]
   \draw[#1, xshift  = -\tkz@x@delta cm,]
         plot[samples = \tkz@fct@samples,id=\tkz@fct@id,
         domain= \tkz@ba:\tkz@bb] function{((#2)-\tkz@init@yorigine)/\tkz@init@ystep};
  % \node {-\tkz@y@delta cm };
\end{scope}
% %%%%%%%
   \let\tkz@tmp@xstep\tkz@init@xstep
   \def\tkz@init@xstep{1}
\iftkz@fp%       
   \ReplaceSubStrings{\tkz@tempa}{#2}{log}{ln}
   \ReplaceSubStrings{\tkz@tempb}{\tkz@tempa}{**}{^}
   \ReplaceSubStrings{\tkz@tempa}{\tkz@tempb}{\x}{x}
   \ReplaceSubStrings{\tkz@tempb}{\tkz@tempa}{asin}{arcsin}
   \ReplaceSubStrings{\tkz@tempa}{\tkz@tempb}{acos}{arccos}
   \ReplaceSubStrings{\tkz@tempb}{\tkz@tempa}{atan}{arctan} 
  \expandafter\edef\csname tkzFct\@alph\tkz@tkzf@fct\endcsname{\tkz@tempb}%
  \expandafter\edef\csname tkzFctLast\endcsname{\tkz@tempb}%
 \fi 
  \let\tkz@init@xstep\tkz@tmp@xstep 
 \catcode`\:=\tkzTWOPTCode\relax   
}%
%<--------------------------------------------------------------------------->
%                            point d'une courbe (new method)
%<--------------------------------------------------------------------------->
\pgfkeys{/tkzfctpt/.cd, 
      draw/.is if         = tkz@draw,
      draw/.default       = true,
      with/.store in      = \tkz@fct@used,
      ref/.store in       = \tkz@fct@ref,
            draw          = false,
            with          = {},
            ref           = {}
      }
                           
\def\tkzDefPointByFct{\pgfutil@ifnextchar[{\tkz@fctpoint}{\tkz@fctpoint[]}}   
\def\tkz@fctpoint[#1](#2){%
\pgfqkeys{/tkzfctpt}{#1}
   \FPeval\x{(#2)}%
   \ifx\tkzutil@empty\tkz@fct@used
       \FPeval\tkz@fx{\tkzFctLast}
   \else
      \edef\tkz@fct{\csname tkzFct\tkz@fct@used\endcsname}
      \FPeval\tkz@fx{\tkz@fct}
    \fi
  \tkzDefPoint(\x,\tkz@fx){tkzPointResult}%
  \iftkz@draw
     \tkzDrawPoint(tkzPointResult)
     \fi
\ifx\tkzutil@empty\tkz@fct@ref
\else
  \tkzGetPoint{\tkz@fct@ref}
\fi
}%
%<--------------------------------------------------------------------------->
%                                  tangente \tkzDrawTangentLine
%<--------------------------------------------------------------------------->
\pgfkeys{/@tkztan/.cd,
      with/.store in          = \tkz@fct@used,
      kr/.store in            = \tkz@tan@kr,
      kl/.store in            = \tkz@tan@kl,
      draw/.is if             = tkz@draw,
      draw/.default           = true,
            draw            = false,
            with            = {},   
            kr              = 1,
            kl              = 1,
        /@tkztan/.search also  = {/tikz},
}%
%<--------------------------------------------------------------------------->
\def\tkzDrawTangentLine{\pgfutil@ifnextchar[{\tkz@DrawTangentLine}{%
                                             \tkz@DrawTangentLine[]}} 
\def\tkz@DrawTangentLine[#1](#2){%
\begingroup
\pgfqkeys{/@tkztan}{#1}
\FPeval\x{(#2)}%
\ifx\tkzutil@empty\tkz@fct@used
    \FPeval\tkz@img{\tkzFctLast}
    \let\tkz@fct\tkzFctLast  
\else 
   \edef\tkz@fct{\csname tkzFct\tkz@fct@used\endcsname}
      \FPeval\tkz@img{\tkz@fct}
 \fi
\let\tkz@old@img\tkz@img
\let\tkz@old@x\x
\tkzDefPoint(\tkz@old@x,\tkz@old@img){tkzPointResult}  
\FPdiv{\tkz@krx}{\tkz@tan@kr}{\tkz@init@xstep}
\FPdiv{\tkz@kry}{\tkz@tan@kr}{\tkz@init@ystep}
\FPdiv{\tkz@klx}{\tkz@tan@kl}{\tkz@init@xstep}
\FPdiv{\tkz@kly}{\tkz@tan@kl}{\tkz@init@ystep}
 % demi tgt--> droite
\FPifzero{\tkz@tan@kr}%
\else
\FPadd{\x}{\tkz@old@x}{0.000001}%
\FPeval\tkz@img{\tkz@fct}% 
\FPadd{\tkz@deltay}{\tkz@old@img}{-\tkz@img}%
\FPmul{\tkz@slope}{\tkz@deltay}{1000000}%
\protected@edef\tkz@temp{%
\noexpand\draw[/tkztan/tan style,/@tkztan/.cd,#1]}\tkz@temp%
      (tkzPointResult)--++(\tkz@krx,-\tkz@kry*\tkz@slope);
\fi
% demi tgt --> gauche
\FPifzero{\tkz@tan@kl}%
 \else
\FPadd{\x}{\tkz@old@x}{-0.000001} 
\FPeval\tkz@img{\tkz@fct}% 
\FPadd{\tkz@deltay}{\tkz@old@img}{-\tkz@img}
\FPmul{\tkz@slope}{\tkz@deltay}{-1000000}
\protected@edef\tkz@temp{%
\noexpand\draw[/tkztan/tan style,/@tkztan/.cd,#1]}\tkz@temp%
     (tkzPointResult)--++(-\tkz@klx,\tkz@kly*\tkz@slope);
  \fi
     \iftkz@draw
     \tkzDrawPoint(tkzPointResult)
     \fi  
\endgroup 
}
%<--------------------------------------------------------------------------->
%                               tkzArea
%<--------------------------------------------------------------------------->
\pgfkeys{%
/tkzarea/.cd,
        domain/.code args={#1:#2}         {\FPeval\tkz@min{(#1)}
                                           \FPeval\tkz@max{(#2)}}, 
        opacity/.store in               =  \tkz@fct@opacity,
        id/.store in                    =  \tkz@fct@id,
        color/.store in                 =  \tkz@fct@color,
        samples/.store in               =  \tkz@fct@samples,
        domain                = -5:5,   
        color                 =  \tkz@otherlinecolor,
        samples               =  200,
        id                    =  tkzfonct,
        opacity               =  .5,
          /tkzarea/.search also  = {/tikz}
                   }
\def\tkzArea{\pgfutil@ifnextchar[{\catcode`\:=12 \tkz@aire}{\tkz@aire[]}}
\def\tkz@aire[#1]{%
\begingroup   
\pgfqkeys{/tkzarea}{#1}
\iftkz@init@NO
   \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
   \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
\else        
   \FPset{\tkz@x@delta}{0}
   \FPset{\tkz@y@delta}{0}%
\fi%
\def\x{(x*\tkz@init@xstep)}
\FPdiv\tkz@ba{\tkz@min}{\tkz@init@xstep}%
\FPdiv\tkz@bb{\tkz@max}{\tkz@init@xstep}%
\begin{scope}[] 
  \path[ fill,  
         xshift       = -\tkz@x@delta cm,
         yshift       = -\tkz@y@delta cm,
         color        = \tkz@fct@color,%
         fill opacity = \tkz@fct@opacity,#1,
         domain       = \tkz@ba:\tkz@bb,
         samples      = \tkz@fct@samples]%
        (\tkz@ba,0)--plot [id = \tkz@fct@id]%
       function{(\tkzFctgnuLast)/\tkz@init@ystep}--(\tkz@bb,0);
   \end{scope}
\endgroup
}
\let\tkzDrawArea\tkzArea 

%<--------------------------------------------------------------------------->
%                             tkzAreafg
%<--------------------------------------------------------------------------->
\pgfkeys{/@tkzareafg/.cd,
    domain/.code args         ={#1:#2} {\FPeval\tkz@min{(#1)}
                                        \FPeval\tkz@max{(#2)}},
    between/.code args        ={#1 and #2}{\def\tkzfct@below{#1}
                                           \def\tkzfct@above{#2}},
    opacity/.store in         =  \tkz@fct@opacity,
    id/.store in              =  \tkz@fct@id,
    color/.store in           =  \tkz@fct@color,
    samples/.store in         =  \tkz@fct@samples,
    domain                    =  -5:5,
    between                   =  a and b,
    color                     =  lightgray,
    samples                   =  200,
    id                        =  tkzfonct,
    opacity                   =  .5,
  /@tkzareafg/.search also  = {/tikz}
                   }

\def\tkzAreafg{\pgfutil@ifnextchar[{\tkz@airefg}{\tkz@airefg[]}}
\def\tkz@airefg[#1]{%
\begingroup
\pgfqkeys{/@tkzareafg}{#1}
\def\tkz@fctsup{\csname tkzFctgnu\tkzfct@below\endcsname}
\def\tkz@fctinf{\csname tkzFctgnu\tkzfct@above\endcsname}
\iftkz@init@NO%
   \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
   \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@xstep}%
\else
   \FPset{\tkz@x@delta}{0}
   \FPset{\tkz@y@delta}{0}%
\fi%
% essai du test sur xstep
\def\x{(x*\tkz@init@xstep)}
\FPdiv\tkz@ba{\tkz@min}{\tkz@init@xstep}
\FPdiv\tkz@bb{\tkz@max}{\tkz@init@xstep}
\begin{scope}[domain  = \tkz@ba:\tkz@bb,
              samples = \tkz@fct@samples,
              id      = \tkz@fct@id]
 \clip (\tkz@ba,\tkz@ya)--plot function{(\tkz@fctsup)/\tkz@init@ystep}%
                        --(\tkz@bb,\tkz@ya);%
 \clip (\tkz@ba,\tkz@yb)--plot function{(\tkz@fctinf)/\tkz@init@ystep}%
                        --(\tkz@bb,\tkz@yb);
  \fill [color        = \tkz@fct@color,
         fill opacity = \tkz@fct@opacity,
         /@tkzareafg/.cd,
         #1]%
        (\tkz@ba,\tkz@ya) rectangle (\tkz@bb,\tkz@yb);
\end{scope}
\endgroup
}
\let\tkzDrawAreafg\tkzAreafg
\def\tkz@pamin{-5}\def\tkz@pamax{5}
%<--------------------------------------------------------------------------->
%                               tkzFctPar
%<--------------------------------------------------------------------------->
\def\tkzFctPar{\pgfutil@ifnextchar[{\catcode`\:=12 \tkz@fctp}{\tkz@fctp[]}}
\def\tkz@fctp[#1]#2#3{%
\begingroup
\pgfkeys{/@tkzfct/.cd,
      domain      = \tkz@pamin:\tkz@pamax,
      samples     = 200,
      fp          = true,
      id          = tkzfct}
\pgfqkeys{/@tkzfct}{#1}
  \iftkz@init@NO%
     \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
     \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
  \else
     \FPset{\tkz@x@delta}{0}
     \FPset{\tkz@y@delta}{0}%
  \fi%
 \begin{scope}[samples    = \tkz@fct@samples]
  \tkzActivOff
    \clip (\tkz@xa,\tkz@ya) rectangle (\tkz@xb,\tkz@yb);
    \draw [xshift     = -\tkz@x@delta cm,%
           yshift     = -\tkz@y@delta cm,
           /@tkzfct/.cd, #1]%
     plot [parametric,domain     = \tkz@min:\tkz@max,%
           id         = tkzparfct]%
         function {(#2)/\tkz@init@xstep,(#3)/\tkz@init@ystep};
  \tkzActivOn
  \end{scope}
\endgroup
}
%<--------------------------------------------------------------------------->
%                               tkzFctPolar
%<--------------------------------------------------------------------------->
\def\tkz@pomin{0}\def\tkz@pomax{2*pi}
\def\tkzFctPolar{\pgfutil@ifnextchar[{%
                 \catcode`\:=12 \tkz@FctPolar}{\tkz@FctPolar[]}}
\def\tkz@FctPolar[#1]#2{%
\begingroup
\pgfkeys{/@tkzfct/.cd,
      domain      = \tkz@pomin:\tkz@pomax,
      fp          = true,
      samples     = 200,
      id          = tkzfctpolar
      }
\pgfqkeys{/@tkzfct}{#1}
  \iftkz@init@NO%
     \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
     \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
  \else
     \FPset{\tkz@x@delta}{0}
     \FPset{\tkz@y@delta}{0}%
  \fi%

  \begin{scope}
  \tkzActivOff
    \draw [xshift     = -\tkz@x@delta cm,%
           yshift     = -\tkz@y@delta cm,
           /@tkzfct/.cd, #1]%
     plot [raw gnuplot,id = tkzpolarfct]%
         function { set polar;set samples \tkz@fct@samples ; set trange  [\tkz@min:\tkz@max]; plot (#2)/\tkz@init@xstep};
  \tkzActivOn
  \end{scope}
\endgroup
}
%<--------------------------------------------------------------------------->
%                               tkzDrawRiemannSum
%<--------------------------------------------------------------------------->
\pgfkeys{/@tkzriemann/.cd,
      interval/.code args={#1:#2} {\FPeval\tkz@min{(#1)}
                                   \FPeval\tkz@max{(#2)}},
      number/.store in                =  \tkz@fct@nb,
      opacity/.store in               =  \tkz@fct@opacity,
      color/.store in                 =  \tkz@fct@color,
 /@tkzriemann/.search also  = {/tikz},
}
\def\tkzDrawRiemannSum{\pgfutil@ifnextchar[{%
    \catcode`\:=12 \tkz@DrawRiemannSum}{\tkz@DrawRiemannSum[]}}
\def\tkz@DrawRiemannSum[#1]{%
\begingroup
\pgfkeys{/@tkzriemann/.cd,
    interval        = 1:2,
    number          = 10,
    opacity         = 0.5,
    line width      = 1pt
}
\pgfqkeys{/@tkzriemann}{#1}
  \iftkz@init@NO%
     \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
     \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
  \else
     \FPset{\tkz@x@delta}{0}
     \FPset{\tkz@y@delta}{0}%
  \fi%
\FPadd\tkz@intwd{\tkz@max}{-\tkz@min}
\FPdiv\tkz@delta{\tkz@intwd}{\tkz@fct@nb}
\FPadd\tkz@fct@nb{\tkz@fct@nb}{-1}
 \begin{scope}
\foreach \i in {0,1,...,\tkz@fct@nb}{%
  \FPeval\x{(\tkz@min+i*\tkz@delta)}
  \FPeval\tkz@fx{\tkzFcta}
  \FPadd\tkz@next{\x}{\tkz@delta}
  \draw[ color=\tkz@fct@color,
     opacity=\tkz@fct@opacity,
     /@tkzriemann/.cd,#1] (\x ,0 ) rectangle (\tkz@next,
     \tkz@fx/\tkz@init@ystep );
}
   \end{scope}
\endgroup
}
%<--------------------------------------------------------------------------->
%                               tkzDrawRiemannSumInf
%<--------------------------------------------------------------------------->
\pgfkeys{/@tkzriemann/.cd,
      interval/.code args={#1:#2} {\FPeval\tkz@min{(#1)}
                                   \FPeval\tkz@max{(#2)}},
      number/.store in                =  \tkz@fct@nb,
      opacity/.store in               =  \tkz@fct@opacity,
      color/.store in                 =  \tkz@fct@color,
      /@tkzriemann/.search also   = {/tikz},
                   }
\def\tkzDrawRiemannSumInf{\pgfutil@ifnextchar[{%
                 \catcode`\:=12 \tkz@DrawRiemannSumInf}{\tkz@DrawRiemannSumInf[]}}
\def\tkz@DrawRiemannSumInf[#1]{%
\begingroup
\pgfkeys{/@tkzriemann/.cd,
    interval        = 1:2,
    number          = 10,
    opacity         = 0.5,
    line width      = 1pt
    }
\pgfqkeys{/@tkzriemann}{#1}
  \iftkz@init@NO%
     \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
     \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
  \else
     \FPset{\tkz@x@delta}{0}
     \FPset{\tkz@y@delta}{0}%
  \fi%
\FPadd\tkz@intwd{\tkz@max}{-\tkz@min}
\FPdiv\tkz@delta{\tkz@intwd}{\tkz@fct@nb}
\FPadd\tkz@fct@nb{\tkz@fct@nb}{-1}

\begin{scope}
\foreach \i in {0,1,...,\tkz@fct@nb}{%
  \FPeval\x{(\tkz@min+i*\tkz@delta)}
  \FPeval\tkz@fx{\tkzFcta}
   \let\tkz@firstimg\tkz@fx
   \let\tkz@firstx\x
   \FPadd\tkz@next{\x}{\tkz@delta}
   \let\tkzFctTmp\tkzFcta
   \FPset\x{\tkz@next}
   \FPeval\tkz@fxnext{\tkzFcta}
   \ifdim \tkz@fx pt < \tkz@fxnext pt\relax
   \draw[opacity=\tkz@fct@opacity,/@tkzriemann/.cd,#1] (\tkz@firstx ,0 ) rectangle (\tkz@next , \tkz@firstimg/\tkz@init@ystep );
\else
\draw[opacity=\tkz@fct@opacity,/@tkzriemann/.cd,#1] (\tkz@firstx ,0 ) rectangle (\tkz@next , \tkz@fxnext/\tkz@init@ystep );
 \fi
   }
   \end{scope}
\endgroup
}
%<--------------------------------------------------------------------------->
%                               tkzDrawRiemannSumSup
%<--------------------------------------------------------------------------->

\def\tkzDrawRiemannSumSup{\pgfutil@ifnextchar[{%
                 \catcode`\:=12 \tkz@DrawRiemannSumSup}{\tkz@DrawRiemannSumSup[]}}
\def\tkz@DrawRiemannSumSup[#1]{%
\begingroup
\pgfkeys{/@tkzriemann/.cd,
      interval        = 1:2,
      number          = 10,
      opacity         = 0.5,
      line width      = 1pt
      }
\pgfqkeys{/@tkzriemann}{#1}
  \iftkz@init@NO%
     \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
     \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
  \else
     \FPset{\tkz@x@delta}{0}
     \FPset{\tkz@y@delta}{0}%
  \fi%
\FPadd\tkz@intwd{\tkz@max}{-\tkz@min}
\FPdiv\tkz@delta{\tkz@intwd}{\tkz@fct@nb}
\FPadd\tkz@fct@nb{\tkz@fct@nb}{-1}

\begin{scope}
\foreach \i in {0,1,...,\tkz@fct@nb}{%
  \FPeval\x{(\tkz@min+i*\tkz@delta)}
  \FPeval\tkz@fx{\tkzFcta}
   \let\tkz@firstimg\tkz@fx
    \let\tkz@firstx\x
  \FPadd\tkz@next{\x}{\tkz@delta}
  \let\tkzFctTmp\tkzFcta
  \FPset\x{\tkz@next}
  \FPeval\tkz@fxnext{\tkzFcta}
\ifdim \tkz@fx pt > \tkz@fxnext pt\relax
 \draw[opacity=\tkz@fct@opacity,/@tkzriemann/.cd,#1] (\tkz@firstx ,0 ) rectangle (\tkz@next , \tkz@firstimg/\tkz@init@ystep );
\else
\draw[opacity=\tkz@fct@opacity,/@tkzriemann/.cd,#1] (\tkz@firstx ,0 ) rectangle (\tkz@next , \tkz@fxnext/\tkz@init@ystep );
 \fi
   }
   \end{scope}
\endgroup
}
%<--------------------------------------------------------------------------->
%                               tkzDrawRiemannSumMid
%<--------------------------------------------------------------------------->

\def\tkzDrawRiemannSumMid{\pgfutil@ifnextchar[{%
                 \catcode`\:=12 \tkz@DrawRiemannSumMid}{\tkz@DrawRiemannSumMid[]}}
\def\tkz@DrawRiemannSumMid[#1]{%
\begingroup
\pgfkeys{/@tkzriemann/.cd,
    interval        = 1:2,
    number          = 10,
    opacity         = 0.5,
    line width      = 1pt
    }
\pgfqkeys{/@tkzriemann}{#1}
  \iftkz@init@NO%
     \FPdiv{\tkz@x@delta}{\tkz@init@xorigine}{\tkz@init@xstep}%
     \FPdiv{\tkz@y@delta}{\tkz@init@yorigine}{\tkz@init@ystep}%
  \else
     \FPset{\tkz@x@delta}{0}
     \FPset{\tkz@y@delta}{0}%
  \fi%
\FPadd\tkz@intwd{\tkz@max}{-\tkz@min}
\FPdiv\tkz@delta{\tkz@intwd}{\tkz@fct@nb}
\FPadd\tkz@fct@nb{\tkz@fct@nb}{-1}

\begin{scope}
\foreach \i in {0,1,...,\tkz@fct@nb}{%
  \FPeval\x{(\tkz@min+i*\tkz@delta)}
  \FPeval\tkz@fx{\tkzFcta}
   \let\tkz@firstimg\tkz@fx
    \let\tkz@firstx\x
  \FPadd\tkz@next{\x}{\tkz@delta}
  \FPset\x{\tkz@next}
  \FPeval\tkz@fxnext{\tkzFcta}
  \FPeval\tkz@midimg{(\tkz@firstimg+\tkz@fxnext)/2}

 \draw[opacity=\tkz@fct@opacity,/@tkzriemann/.cd,#1] (\tkz@firstx ,0 ) rectangle (\tkz@next , \tkz@midimg/\tkz@init@ystep );
   }
   \end{scope}
\endgroup
}

%<--------------------------------------------------------------------------–>
%                 tkzXH      F rien T et [ ou ]
%<--------------------------------------------------------------------------–>
\def\tkz@ComT{v}  % need

\pgfkeys{/@tkzxh/.cd,
     posgrad/.store in  = \tkz@tkzXh@posgrad,
     left/.store in     = \tkz@tkzXh@left,
     right/.store in    = \tkz@tkzXh@right,
     color/.store in    = \tkz@tkzXh@color,
     posgrad = {below=6 pt},
     color   = \tkz@mainlinecolor,
     left    = {},
     right   = {},
  }
\def\tkzXH{\pgfutil@ifnextchar[{\tkz@xh}{\tkz@xh[]}}
\def\tkz@xh[#1]#2{%
 \pgfqkeys{/@tkzxh}{#1}
\begingroup
\foreach \xA/\RA/\sA/\xB/\RB/\sB in {#2}{%
       \tkzDefPoint(\xA,0){deb}\tkzDefPoint(\xB,0){fin}
       \begin{scope}[decoration={border,angle=-45,
                    amplitude=0.3cm,segment length=2mm,raise=0.1cm}]
         \draw[decorate,\tkz@tkzXh@color,thick] (deb) -- (fin);
         \node at (deb) {\Large\textbf{\sA}};
         \node at (fin) {\Large\textbf{\sB}};
\ifx\RA\tkz@ComT
 \protected@edef\tkz@temp{%
  \noexpand\node[\tkz@tkzXh@posgrad]}\tkz@temp at (deb){%
 \tkz@showgrad{\xA}$\numprint{\tkz@result}$};\fi

\ifx\RB\tkz@ComT
  \protected@edef\tkz@temp{\noexpand\node[\tkz@tkzXh@posgrad]}\tkz@temp%
          at (fin){%
   \tkz@showgrad{\xB}$\numprint{\tkz@result}$};\fi

\protected@edef\tkz@temp{%
          \noexpand\node[\tkz@tkzXh@posgrad]}\tkz@temp at (\tkz@xa,0) {\tkz@tkzXh@left};%
\protected@edef\tkz@temp{\noexpand\node[\tkz@tkzXh@posgrad]}\tkz@temp%
 at (\tkz@xb,0) {\tkz@tkzXh@right};
\end{scope}
}%
\endgroup
}%
% %<--------------------------------------------------------------------------->
% %                                  tangente  obsolete
% %<--------------------------------------------------------------------------->
% \def\tkzTan{\pgfutil@ifnextchar[{\tkz@Tan}{\tkz@Tan[]}}
%
% \def\tkz@Tan[#1]#2(#3)#{
% \pgfkeys{/@tkztan/.cd,
%     draw  =   false,
%     with  = {},
%     kr    =   1,
%     kl    =   1
%     }
% \pgfqkeys{/@tkztan}{#1}
%  \global\edef\tkz@opttan{#1}
%   \FPeval\x{(#3)}%
%   \FPeval\tkz@img{#2}
%   \let\tkz@yy #2%
%   \let\tkz@xx\x%
%   \let\tkz@old@img\tkz@img
%   \tkz@@Tan
% }
% \def\tkz@@Tan#1{%
% \FPdiv{\tkz@old@x}{\x}{1}
% \let\tkz@nexpty\tkz@img
% \tkz@ptStar[](\tkz@old@x,\tkz@nexpty){#1}
% \FPdiv{\tkz@krx}{\tkz@tan@kr}{\tkz@tmp@xstep}
% \FPdiv{\tkz@kry}{\tkz@tan@kr}{\tkz@init@ystep}
% \FPdiv{\tkz@klx}{\tkz@tan@kl}{\tkz@tmp@xstep}
% \FPdiv{\tkz@kly}{\tkz@tan@kl}{\tkz@init@ystep}
%  % demi tgt--> droite
% \FPifzero{\tkz@tan@kr}%
% \else
% \FPadd{\x}{\tkz@xx}{0.000001}%
% \FPeval\tkz@img{\tkz@yy}%
% \FPadd{\tkz@deltay}{\tkz@old@img}{-\tkz@img}%
% \FPmul{\tkz@slope}{\tkz@deltay}{1000000}%
% \protected@edef\tkz@temp{
%    \noexpand\draw[tan style,/@tkztan/.cd,\tkz@opttan,-]}\tkz@temp%
%        (#1)--++(\tkz@krx,-\tkz@kry*\tkz@slope);
% \fi
% % demi tgt --> gauche
% \FPifzero{\tkz@tan@kl}%
%  \else
% \FPadd{\x}{\tkz@xx}{-0.000001}
% \FPeval\tkz@img{\tkz@yy}%
% \FPadd{\tkz@deltay}{\tkz@old@img}{-\tkz@img}
% \FPmul{\tkz@slope}{\tkz@deltay}{-1000000}
%   \protected@edef\tkz@temp{
%     \noexpand
% \draw[tan style,/@tkztan/.cd,\tkz@opttan,-]}\tkz@temp%
%        (#1)--++(-\tkz@klx,\tkz@kly*\tkz@slope);
%   \fi
% }
%<--------------------------------------------------------------------------->
%                            point d'une courbe   obsolete
%<--------------------------------------------------------------------------->
\def\tkzFctPt{\pgfutil@ifnextchar[{\tkz@fctpt}{\tkz@fctpt[]}}
\def\tkz@fctpt[#1]#2(#3)#{%
 \global\edef\tkz@optptfct{#1}
  \FPeval\x{(#3)}%
  \FPeval\tkz@y{#2}
  \tkz@defpoint
}
\def\tkz@defpoint#1{
  \tkzDefPoint(\x,\tkz@y){#1}
\protected@edef\tkz@temp{
   \noexpand  \tkzDrawPoint[\tkz@optptfct](#1)}\tkz@temp
  \let\tkz@init@xstep\tkz@tmp@xstep
}
%<--------------------------  the end --------------------------------------->
\endinput 

 