Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image Discussion #120

Open
ghost opened this issue Jul 27, 2019 · 4 comments
Open

Image Discussion #120

ghost opened this issue Jul 27, 2019 · 4 comments

Comments

@ghost
Copy link

ghost commented Jul 27, 2019

This is mostly for Alex and Sean to discuss but I thought I would create it here since it seems like it would be a good place for it. There is a small issue with the way borders work with filling with the new method Alex provided. In the current preamble there is a line:

\pgfplotsset{firstcurvestyle/.style={color=firstcolor,mark=none,thick,{Kite}-{Kite},solid}}

which was updated to add the ability to fill without needing to use 2 separate addplot commands (the color information was added in here instead, before it was defined in the addplot function using {\coloronefill} which was set to: blue!15!white) as shown below.

\pgfplotsset{firstcurvestyle/.style={color=firstcolor,mark=none,thick,{Kite}-{Kite},solid,fill=firstcolor!50,fill=none}}

Now, it is possible to convert an image like this one (from sec_improper_integration: img_impint1a):

\begin{tikzpicture}

\begin{axis}[
axis on top,
xtick={1,5,10},
ymin=-.1,ymax=1.1,
xmin=-1,xmax=11
]

\addplot [{\coloronefill},fill={\coloronefill}] coordinates {(1.,1.)(1.5,0.4444)(2.,0.25)(2.5,0.16)(3.,0.1111)(3.5,0.08163)(4.,0.0625)(4.5,0.04938)(5.,0.04)(5.5,0.03306)(6.,0.02778)(6.5,0.02367)(7.,0.02041)(7.5,0.01778)(8.,0.01563)(8.5,0.01384)(9.,0.01235)(9.5,0.01108)(10.,0.01)(10.5,0.009)} -- (axis cs:10.5,0)--(axis cs:1,0)--cycle;
\addplot [{\colorone},thick,smooth] coordinates {(1.,1.)(1.5,0.4444)(2.,0.25)(2.5,0.16)(3.,0.1111)(3.5,0.08163)(4.,0.0625)(4.5,0.04938)(5.,0.04)(5.5,0.03306)(6.,0.02778)(6.5,0.02367)(7.,0.02041)(7.5,0.01778)(8.,0.01563)(8.5,0.01384)(9.,0.01235)(9.5,0.01108)(10.,0.01)(10.5,0.009)};

\draw (axis cs:5,.75) node {\scriptsize $\ds f(x)=\frac{1}{x^2}$};

\end{axis}

\end{tikzpicture}

Which looks like this (matching the original in the book):
image

into:

\begin{tikzpicture}

\begin{axis}[
axis on top,
xtick={1,5,10},
ymin=-.1,ymax=1.1,
xmin=-1,xmax=11
]

\addplot+ [fill,smooth] coordinates {(1.,1.)(1.5,0.4444)(2.,0.25)(2.5,0.16)(3.,0.1111)(3.5,0.08163)(4.,0.0625)(4.5,0.04938)(5.,0.04)(5.5,0.03306)(6.,0.02778)(6.5,0.02367)(7.,0.02041)(7.5,0.01778)(8.,0.01563)(8.5,0.01384)(9.,0.01235)(9.5,0.01108)(10.,0.01)(10.5,0.009)} -- (axis cs:10.5,0)--(axis cs:1,0)--cycle;

\draw (axis cs:5,.75) node {\scriptsize $\ds f(x)=\frac{1}{x^2}$};

\end{axis}

\end{tikzpicture}

Which now looks like this image below.
image

Notice that there is a border around the fill. I believe the issue might lie in the cycle code at the end of the image such as:

-- (axis cs:10.5,0)--(axis cs:1,0)--cycle

in the above case or

\closedcycle

in other cases that appear in chapter 5.

As an aside for Sean (since we are on the topic). We have not discussed it but notice that the image above is using coordinates to define its curve when the function is obvious. In chapter 5 the images are updated to use functions and samples instead. I can easily convert the image above to use that form in most cases I have seen (I have kind of done some already in chapter 6, couldn't resist). The example above when converted will be:

\begin{tikzpicture}

\begin{axis}[
axis on top,
xtick={1,5,10},
ymin=-.1,ymax=1.1,
xmin=-1,xmax=11
]

\addplot [{\coloronefill},fill={\coloronefill},domain=1:10.5,samples=100] {1/(x^2)} -- (axis cs:10.5,0) -- (axis cs:1,0) -- cycle;
\addplot [firstcurvestyle,-,domain=1:10.5,samples=100] {1/(x^2)};

\draw (axis cs:5,.75) node {\scriptsize $\ds f(x)=\frac{1}{x^2}$};

\end{axis}

\end{tikzpicture}

and if using the updated command I can shorten it to the code below.

\begin{tikzpicture}

\begin{axis}[
axis on top,
xtick={1,5,10},
ymin=-.1,ymax=1.1,
xmin=-1,xmax=11
]

\addplot+ [fill,domain=1:10.5,samples=100] {1/(x^2)} \closedcycle;

\draw (axis cs:5,.75) node {\scriptsize $\ds f(x)=\frac{1}{x^2}$};

\end{axis}

\end{tikzpicture}

This is much shorter and definitely something I want to do. Of course the above only works if we fix the bordering issue and I would have to go back through the images in chapter 5 and some of 6 to support the change.

My question is do you want me making those kinds of changes?

@Alex-Jordan
Copy link
Member

Alex-Jordan commented Jul 27, 2019 via email

@ghost
Copy link
Author

ghost commented Jul 28, 2019

Before I go into a bit of detail below I should state that I am not an experienced mathematician so tricks like the above are likely impossible for me to do given the time Sean has available for me.

Firstly, that is a cool trick for certain hyperbolic functions and it definitely does same some space. I did some testing with samples and found that they seem to be inconsistent with how much they tend to add or subtract. When doing very small changes (<10) it will remove some size occasionally even when adding a few points. But on average it seems to be around 200-300 additional bytes per chunk of 10 with the difference from the default 20 and 50 being about 600-800 bytes.

I also did a full compile and with the images and html (including knowls) the size for me with all 13 chapters was about 32MB. If I had to estimate with around 440 lines of coordinate defined lines with a rough average of 50 samples per line (from a previously varying amount of maybe 10-30) it is around 350KB of extra data. I also found sampling points are not really needed on area fill code in many cases so that will reduce the size as well. I also found replacing smooth with nothing or a small amount of samples, around 40 where needed, reduced the file size by around 1600 bytes on the couple I tried. We could also optimize many of the over sampled images currently in the code base to a degree but I couldn't really guess an amount as they can vary a lot (some might even need to be increased).

I think this would be a much faster approach for development time and could even save additional space to what we have already as images are a very large portion of the data in the book itself. I am actually pretty impressed with how small it is currently. I would however consider even an extra MB or 2 for a considerably quicker and smoother overhaul experience a fair trade-off.

I guess what I am trying to say is that we should look for different places to optimize the compiled size of the code not to mention the readability/maintainability of the code itself. Sean and I are also planning to add some interactivity to the code and it may very well add a ton of extra complexity and space (we have not done too much research on this yet so I cannot say for certain).

On another note I do really like the fill changes and it definitely is the way to go for both code and space efficiency and will be updating everything I can from where we currently are in chapters 5 and 6, both partially done as of tonight.

@ghost
Copy link
Author

ghost commented Jul 30, 2019

I was messing around and I noticed a pretty simple way is to use the domain to split the graphs up into multiple sections so we can concentrate the points where it matters. For instance we can take

\begin{tikzpicture}

\begin{axis}[
axis on top,
xtick={1,5,10},
ymin=-.1,ymax=.5,
xmin=-1,xmax=11
]

\addplot [firstcurvestyle,areastyle,domain=1:10.5,samples=100] {ln(x)/x^2} \closedcycle;
\addplot [firstcurvestyle,-,domain=1:10.5,samples=100] {ln(x)/x^2};

\draw (axis cs:5,.3) node {\scriptsize $\ds f(x)=\frac{\ln(x) }{x^2}$};

\end{axis}

\end{tikzpicture}

and convert it to

\begin{tikzpicture}

\begin{axis}[
axis on top,
xtick={1,5,10},
ymin=-.1,ymax=.5,
xmin=-1,xmax=11
]

\addplot [firstcurvestyle,areastyle,domain=1:2] {ln(x)/x^2} \closedcycle;
\addplot [firstcurvestyle,areastyle,domain=2:10.5] {ln(x)/x^2} \closedcycle;

\addplot [firstcurvestyle,-,domain=1:2] {ln(x)/x^2};
\addplot [firstcurvestyle,-,domain=2:10.5] {ln(x)/x^2};

\draw (axis cs:5,.3) node {\scriptsize $\ds f(x)=\frac{\ln(x) }{x^2}$};

\end{axis}

\end{tikzpicture}

Now we can have a clean curve with much less sampling points at a small cost of extra code. I don't think we lose any maintainability or readability since the code is very simple and the only change is the domain. Also the curves in my above example were way over sampled anyways, that particular example didn't require anything close to what I put. A bit of practice helped with that.

@sean-fitzpatrick
Copy link

This seems reasonable. In some cases you might be able to optimize with a clever substitution, but for now efficiency (w.r.t developer time) might take priority over efficiency (w.r.t. image size).
I'll try to get a few sections of Chapter 6 done today to see how they look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants