# What is the safe PostScript expression to plot 1/floor(1/x)?

What is the safe PostScript expression to plot f(x)=1/floor(1/x)?

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\pstVerb{/inv {dup 0 eq {pop e30} {1 exch div} ifelse} bind def}
\begin{document}
\begin{pspicture}(-3,-3)(5,5)
\psaxes{->}(0,0)(-3,-3)(5,5)
\psplot[linecolor=blue,linewidth=2pt]{-1}{4}{x inv floor inv}% plotting f(x)=1/floor(1/x)
\end{pspicture}
\end{document}

-
For every complex problem, there is a solution that is simple, neat, but wrong. –  Oh my ghost Mar 30 at 13:29
Could you please explain what you mean by safe? It is not clear to me, what you are asking for. Especially with your comments to the existing answers. –  Christoph Mar 30 at 14:35
@Christoph: "save" means the operator inv can handle division by zero smartly and mathematically. –  Oh my ghost Mar 30 at 15:03
@WeirdstressFunction there is no general "mathematically" defined definition that will give finite values that can be plotted: you must define the function that you want to plot. –  David Carlisle Mar 30 at 15:58

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\begin{document}
\psset{unit=4}
\begin{pspicture}(-1.5,-1.2)(1.2,1.2)
\psaxes[Dx=0.5]{->}(0,0)(-1.5,-1.1)(1.1,1.1)
\psplot[algebraic,linecolor=red,VarStep,VarStepEpsilon=1.e-9]{-1.5}{-0.001}{ 1/floor(1/x) }
\psplot[algebraic,linecolor=red,VarStep,VarStepEpsilon=1.e-9]{0.001}{1}{ 1/floor(1/x) }
\end{pspicture}
\end{document}


and plotted as a "function"

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\makeatletter
\def\doqp@line{ 2 copy CP Pyth2 1 gt { moveto }{ L } ifelse }
\makeatother
\begin{document}
\psset{unit=4}
\begin{pspicture}(-1.5,-1.2)(1.2,1.2)
\psaxes[Dx=0.5]{->}(0,0)(-1.5,-1.1)(1.1,1.1)
\psset{algebraic,linecolor=red,VarStep,VarStepEpsilon=1.e-9,linewidth=1.5pt}
\psplot{-1.5}{-0.001}{ 1/floor(1/x) }
\psplot{0.001}{1}{ 1/floor(1/x) }
\end{pspicture}
\end{document}


-
+1 I will be nice if the vertical lines are not drawn. –  Oh my ghost Mar 31 at 8:51

Your inv definition didn't appear to be in scope so I just inlined a similar definition

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\pstVerb{/inv {dup 0 eq {pop e30} {1 exch div} ifelse} bind def}
\begin{document}
\begin{pspicture}(-3,-3)(5,5)
\psaxes{->}(0,0)(-3,-3)(5,5)
\psplot[linecolor=blue,linewidth=2pt]{-1}{4}
{1 1 x 0 eq {0.000001} {x} ifelse  div floor dup 0 eq {pop 0.000001} {} ifelse div}% plotting f(x)=1/floor(1/x)
\end{pspicture}
\end{document}

-
@WeirdstressFunction you asked for safe not correct:-) if x>1 floor(1/x) is 0 so 1/floor(1/x) is a bit arbitrary, what do you want it to do? –  David Carlisle Mar 30 at 13:27
@WeirdstressFunction The function isn't defined over the specified domain so some mathematicians (including this one) would say that no output at all is an acceptable outcome. I changed the function to replace 0 by an arbitrary small value, if you want another outcome you need to specify the function at all values in the domain. –  David Carlisle Mar 30 at 14:35

It's 1 1 x div floor div. I don't know if the floorfunction is defined for algebraic notation in pstricks. Here is a code to plot this function with pstricks to be compiled with pdflatex; due to the infinite discontinuity in 0, you have to plot the two parts of the curve separately:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{MinionPro}
\pagestyle{empty}
\usepackage[pdf, svgnames]{pstricks}%
\def\myfunc{1 1 x div floor div}

\begin{document}

\psset{unit = 5cm, plotpoints = 1000}
\begin{pspicture}
\psaxes{->}(0,0)(-1.5,-1.5)(1.5,1.5)
%\psplot[linecolor = VioletRed]{0.1}{1}{1 1 x div floor div}
\psset{linecolor = VioletRed}
\psplot[arrows = -*]{0.002}{1}{\myfunc}
\psplot[arrows = *-]{-1}{-0.002}{\myfunc}
\pscircle[linewidth = 0.25pt, fillstyle = solid, fillcolor = white](0,0){0.8pt}
\end{pspicture}

\end{document}


Note : this isn't the real mathematical curve, as the vertical segments are not part of it. Plots obtained with \psplot are always continuous curves, which this one is not. Plotting the real curve would be more complex.

-
The domain is not complete. It must span from a negative x to a positive x. –  Oh my ghost Mar 30 at 13:09
In only gave the principle. My answer is updated. –  Bernard Mar 30 at 13:40