如何在 Latex 中绘制 π(x)(素函数)的图像?

如何在 Latex 中绘制 π(x)(素函数)的图像?

我想在 Latex 中绘制 π(x) 的图形,然后找到了这段代码

\documentclass[tikz,ignorerest=false, border=12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}% 1.13 seems to be very recent

% generated a random strictly increasing sequence of 30 integers
% for the purpose of testing

\pdfsetrandomseed 1234

\makeatletter
\newcommand*\seqA {}%
\edef\@tempa {\pdfuniformdeviate5}%
\edef\seqA {\@tempa}%
% for very very long lists, there are faster ways.
% but let's not bother here.
\count@ 16
\loop
    \edef\@tempa {\the\numexpr\@tempa+\@ne+\pdfuniformdeviate4}%
    \edef\seqA {\seqA, \@tempa}%
\advance\count@\m@ne
\ifnum\count@>\z@
\repeat

\typeout{\string\seqA\space prepared with meaning: \meaning\seqA}
% with pdfrandomseed=1234
% \seqA prepared with meaning macro:->2, 7, 14, 22, 31, 32, 38, 46, 52, 60, 65, 70, 80, 81, 86, 90, 95, 100, 108, 117, 119, 126, 135, 140, 148, 158, 165, 172, 176, 179

% \CumulCnts expandably constructs pairs (x, \pi_S(x)), 0≤ x ≤ xmax,
% for sequence S, given as comma separated increasing list

% It admits optional argument, default xmax=100 to limit x.

% usage: \CumulCnts[optional max x]{\A} will expand to the list of pairs
% inside an \edef or a \csname...\endcsname.

% \edef\cumlA {\CumulCnts[optional max x]{\A}}
% and then use \cumulA

\newcommand*\CumulCnts {}
\def\CumulCnts #1{\expandafter\CumulCnts@i\romannumeral`\^^@#1,\relax,}%
\def\CumulCnts@i #1{\ifx [#1\expandafter\CumulCnts@opt\else
                            \expandafter\CumulCnts@noopt\fi #1}%
\def\CumulCnts@opt [#1,\relax,#2]#3%
   {\expandafter\CumulCnts@ii 
    \the\numexpr #2\expandafter;\romannumeral`\^^@#3,\relax,}%
\def\CumulCnts@noopt {\CumulCnts@ii 100;}%
\def\CumulCnts@ii    {\CumulCnts@iii 0;0;}%
\def\CumulCnts@iii #1;#2;#3;#4#5,{%
    \if\relax #4\expandafter\CumulCnts@finish\fi
    \ifnum #3<#4#5
        \expandafter\@firstoftwo
    \else
        \expandafter\@secondoftwo
    \fi
    {\CumulCnts@c #1;#2;#3;}
    {\CumulCnts@iv #1;#2;#4#5;{#3}}%
}%
\def\CumulCnts@finish \ifnum #1\fi #2#3{#2\relax,}

\def\CumulCnts@iv #1;#2;#3;{%
    \ifnum #1=#3 \expandafter\CumulCnts@v\fi
    (#1, #2)
    \expandafter\CumulCnts@iv\the\numexpr #1+\@ne;#2;#3;%
}
\def\CumulCnts@v #1;#2;#3;{\expandafter\CumulCnts@vi\the\numexpr #2+\@ne;#3;}%
\def\CumulCnts@vi #1;#2;#3{%
    (#2, #1)
    \expandafter\CumulCnts@iii \the\numexpr#2+\@ne;#1;#3;}%

\def\CumulCnts@c #1;#2;#3;{%
    \ifnum #1>#3 \expandafter\CumulCnts@d\fi
    (#1, #2)
    \expandafter\CumulCnts@c\the\numexpr #1+\@ne;#2;#3;%
}% 
\def\CumulCnts@d #1;#2;#3;#4\relax,{}%

\makeatletter  



\begin{document}

% extending to x≤50
\edef\cumulA {\CumulCnts[50]{\seqA}}                       
\typeout {\string\cumulA\space prepared with meaning: \meaning\cumulA}

\begin{tikzpicture}
\begin{axis}[
  title={Prime counting function: $\pi(x)$},
  xlabel=$x$,
  ylabel=$\pi(x)$,
]
   \addplot[red] coordinates {\cumulA};
\end{axis}
\end{tikzpicture}
\end{document}

然而,这不是素数计数函数的正确图形,因为它应该是这样的在此处输入图片描述

但它就像在此处输入图片描述

求助,我不知道该改什么

答案1

避免处理读取/理解/修改绘制素函数代码的复杂性的简单方法是使用更合适的工具。sagetex 包让您可以访问计算机代数系统 Sage 和 Python 编程语言。Sage 具有内置函数prime_pi(),并有文档记录这里使该问题像其他函数一样易于绘制。请注意,正如文档所述,垂直线不是函数的一部分 - 它们只是帮助函数看起来更好。

\documentclass{standalone}
\usepackage{sagetex}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{sagesilent}
LowerX = 0
UpperX = 50
LowerY = -.1
UpperY = 16
step = .25
t = var('t')
g(t) = prime_pi(t)

x_coords = [t for t in srange(LowerX,UpperX,step)]
y_coords = [g(t).n(digits=5) for t in srange(LowerX,UpperX,step)]

output = r""
output += r"\begin{tikzpicture}"
output += r"\begin{axis}["
output += r"title={The prime counting function: $\pi(x)$},"
output += r"xlabel=$x$,ylabel=$\pi(x)$,"
output += r"xmin=%f,xmax=%f,ymin= %f,ymax=%f,width=10cm]"%(LowerX,UpperX,LowerY, UpperY)
output += r"\addplot[thin, red] coordinates {"
for i in range(0,len(x_coords)-1):
    output += r"(%f , %f) "%(x_coords[i],y_coords[i])
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}

输出运行可钙,如下所示: 在此处输入图片描述

Sage 不是 LaTeX 的一部分,最简单的开始方式是免费的 Cocalc 帐户。

相关内容