Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
For every complex problem, there is a solution that is simple, neat, but wrong. –  Who is crazy first 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. –  Who is crazy first Mar 30 at 15:03
2  
@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

3 Answers 3

up vote 1 down vote accepted
\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}

enter image description here

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}

enter image description here

share|improve this answer
    
+1 I will be nice if the vertical lines are not drawn. –  Who is crazy first 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}
share|improve this answer
2  
@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
2  
@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}%
\usepackage{pstricks-add}
\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} 

enter image description here

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.

share|improve this answer
    
The domain is not complete. It must span from a negative x to a positive x. –  Who is crazy first Mar 30 at 13:09
    
In only gave the principle. My answer is updated. –  Bernard Mar 30 at 13:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.