Hatch and Fill snippets

Hatch and Fill snippets - Functions for decorating area between two functions in 2d plots - Сообщения

#1 Опубликовано: 21.09.2013 05:27:34
Martin Kraska

Martin Kraska

1222 сообщений из 2150 понравились пользователям.

Группа: Moderator

I open this topic for the snippets hatch and fill, as a place for further disussions.

You find these snippets and a corresponding example in the extension manager (online gallery).

Previous discussion can be found here.
hf.PNG
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#2 Опубликовано: 23.09.2013 14:24:31
Fridel Selitsky

Fridel Selitsky

520 сообщений из 451 понравились пользователям.

Группа: User

Example of application

EllipticGears.sm (40 КиБ) скачан 188 раз(а).
3 пользователям понравился этот пост
Вячеслав Мезенцев 14.10.2013 16:30:00, Martin Kraska 23.09.2013 17:22:00, Davide Carpi 23.09.2013 17:46:00
#3 Опубликовано: 11.10.2013 17:58:10
Fridel Selitsky

Fridel Selitsky

520 сообщений из 451 понравились пользователям.

Группа: User


Graf1.smz (2 КиБ) скачан 144 раз(а).
2 пользователям понравился этот пост
Вячеслав Мезенцев 14.10.2013 16:30:00, Martin Kraska 12.10.2013 04:21:00
#4 Опубликовано: 12.10.2013 04:24:04
Martin Kraska

Martin Kraska

1222 сообщений из 2150 понравились пользователям.

Группа: Moderator

It's amazing how compact the code is!
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#5 Опубликовано: 18.10.2013 17:25:49
Fridel Selitsky

Fridel Selitsky

520 сообщений из 451 понравились пользователям.

Группа: User

Two types of shading


Reuleauxtriangle1.sm (34 КиБ) скачан 141 раз(а).
1 пользователям понравился этот пост
Davide Carpi 18.10.2013 19:29:00
#6 Опубликовано: 04.06.2016 23:38:42
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: User

Hi. From the orignal discussion:

Wrote

Fridel, thanks for revisiting the snippet and the performance issue. Quite odd for me: The fastest way seems to be direct element adressing without pre-allocation and avoiding stack(). See attachment. The speed-up is significant.

Please post any response to the new dedicated topic



I understand that stack or augment are bad strategies, but without pre-allocation is better than with pre-allocation? If it is, why is without pre that good. Seems to be to much faster than I can explain or understand.

Best regards.

Alvaro.
#7 Опубликовано: 05.06.2016 02:11:03
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Wrote

I understand that stack or augment are bad strategies, but without pre-allocation is better than with pre-allocation? If it is, why is without pre that good. Seems to be to much faster than I can explain or understand.

Best regards.

Alvaro.



"memory allocation" vs "memory NOT allocation" is confusing to me.
Memory allocales itself from the construction. Visit the "Spot"
"stack" is very fast as being a vector operation. "augment" is slow
as not being a direct vector operation, but very fast within a program.
"hatch" creates a virtual matrix, a scalar matrix, i.e: a matrix at the
plot canvas. Same principle than plotting f(x) on the QuickPlot canvas.
Great construction, though I never used it. Watch it does not face infinity
governed by the N ... at infinity: it crashes Smath !

Jean

Forum Test Derivative.sm (11 КиБ) скачан 109 раз(а).

Utilities Discrete Spot.sm (12 КиБ) скачан 109 раз(а).



#8 Опубликовано: 05.06.2016 02:25:31
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: User

Using stack, if you have a vector, lets says, [x,y,z], then stack(t,[x,y,z]) must to force to push z from 3th plate to 4th, y from 2nd to 3th, ... for finally, put t at first place. And that's can take a very long time.
Preallocating is when you say "let's V:=matrix(20,1)", and then just work defining V[1, V[2 ... etc. If you found that don't need all 20 numbers, just can use submatrix. But if you don't preallocate V, but use V[1 ... V[20, and program work fast, who is preallocating must to be SMath, and that's isn't very efficient for the general cases. Or have some pointer very good for handle that matrices, and I don't know C at that level.

In your example, "Utilities discretes", it is suposed that defining U:=matrix(N,1), and same for V, must to be faster than having U:=0 and then use U[i

Best regards.

Alvaro.
#9 Опубликовано: 05.06.2016 02:41:38
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: User

It is supossed that second algorithm must to be faster than your's, but it isn't.

Best regards.

Alvaro.

SymTime.gif

#10 Опубликовано: 05.06.2016 02:44:51
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

See you tomorrow Alvaro.

That nice application of "hatch" is worth for "typesetting"

Jean

Forum Hatch Demo.gif


#11 Опубликовано: 05.06.2016 03:52:43
Mike Kaganski

Mike Kaganski

184 сообщений из 434 понравились пользователям.

Группа: User

Wrote

I understand that stack or augment are bad strategies, but without pre-allocation is better than with pre-allocation? If it is, why is without pre that good. Seems to be to much faster than I can explain or understand.



Much faster than what? Please provide an example to discuss.

Wrote

Preallocating is when you say "let's V:=matrix(20,1)", and then just work defining V[1, V[2 ... etc. If you found that don't need all 20 numbers, just can use submatrix. But if you don't preallocate V, but use V[1 ... V[20, and program work fast, who is preallocating must to be SMath, and that's isn't very efficient for the general cases. Or have some pointer very good for handle that matrices, and I don't know C at that level.



It's a common practice in libraries to pre-allocate some space for any container. Without this, it would be very inefficient to manage memory. Further, re-allocation strategies on the low level are quite efficient, because they either use % of current size to grow (typical are 2x or golden ratio), or linked lists that don't need reallocation at all. SMath doesn't need to do that itself, because it's .NET that does that for it. And it's VERY efficient for general cases.

Wrote

It is supossed that second algorithm must to be faster than your's, but it isn't.



Well, your slower second time could be explained like this:
pre-allocation in SMath is high-level operation, and it simply adds one additional high-level (slow) operation.

And it could explain something, if not one little thing ... actually it doesn't matter.
SMath calculation time is very volatile, and it's hard to measure time reliably, even doing multiple comparisons/calculations. E.g., processor temperature, that raises towards second calculation, may cause it to slow down to prevent overheating (contemporary systems do that). Some memory leaks in SMath may cause slow-down of all subsequent calculations (and when the sheet is done calculation, .NET garbage collection may clean it up, so recalculation shows the same picture).

Here is a screenshot that shows that your "second" calculation, put first, outperforms original.

scrH52.png
С уважением, Михаил Каганский
#12 Опубликовано: 05.06.2016 12:42:41
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Alvaro, Mike: thanks for entertaining technical matters, very educative.

Though the "algo style" existed in Smath before I joined the team, it
was not elegant and in some ways not visible. As you can see, in the
attached, there is one less "augment" in ... pts(data,spec) compared to
plotG(vx,vy,char,size,color) decreasing the computing time by a factor
of 12 on my testing over 1000 points. You may have better suggestion(s).
For sure welcome in the experiment room .

Jean

Forum Compute Test Points Fill.sm (17 КиБ) скачан 112 раз(а).

Inst_Segment Partial Random.sm (38 КиБ) скачан 110 раз(а).


#13 Опубликовано: 05.06.2016 19:00:11
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: User

Wrote

Wrote

I understand that stack or augment are bad strategies, but without pre-allocation is better than with pre-allocation? If it is, why is without pre that good. Seems to be to much faster than I can explain or understand.



Much faster than what? Please provide an example to discuss.



Than I can explain or understand. Sorry, have not examples of that.


Wrote



It's a common practice in libraries to pre-allocate some space for any container. Without this, it would be very inefficient to manage memory. Further, re-allocation strategies on the low level are quite efficient, because they either use % of current size to grow (typical are 2x or golden ratio), or linked lists that don't need reallocation at all. SMath doesn't need to do that itself, because it's .NET that does that for it. And it's VERY efficient for general cases.



Thanks for the explanation, now I understand why mkraska says "The fastest way seems to be direct element adressing without pre-allocation". Because preallocating is with a high level instruction, and then is slow. Even I have the doubt about why NET pre-allocat V:=0 as matrix when user define that as scalar. I suposse that default SMath types are complexes, not matrices.

So, for the best practices book in SMath, which is best? Never preallocat? Or there are some size when NET take more time than SMath because the NET pre-pre-allocation (the jus't in case one) isn't enough big?

Best regards.

Alvaro.


#14 Опубликовано: 05.06.2016 20:26:09
Mike Kaganski

Mike Kaganski

184 сообщений из 434 понравились пользователям.

Группа: User

Wrote

now I understand why mkraska says "The fastest way seems to be direct element adressing without pre-allocation". Because preallocating is with a high level instruction, and then is slow.



No, I tried to show this. One single allocation instruction's duration is null, zero, nothing. You will be unable to notice it in any measurements in SMath. Martin was not correct. I will say it once more: one method (assigning 0 and then addressing elements) is not faster than the other.

Цитата

Even I have the doubt about why NET pre-allocat V:=0 as matrix when user define that as scalar. I suposse that default SMath types are complexes, not matrices.



No, it "allocates" a scalar (very fast) once, and then re-allocates a vector with a sensible pre-allocated size (very fast) once.

Цитата

So, for the best practices book in SMath, which is best? Never preallocat? Or there are some size when NET take more time than SMath because the NET pre-pre-allocation (the jus't in case one) isn't enough big?



I prefer always preallocating when I know the size. It won't hurt. Its "overhead" of high-level instruction is the same as of the assigning 0 and then re-allocating. In a function, it will not make any difference until the system preallocation size is exceeded, and then, when the function will handle big matrices, it will give you (insignificant, if any) performance gain.
С уважением, Михаил Каганский
1 пользователям понравился этот пост
Alvaro Diaz Falconi 05.06.2016 21:16:00
#15 Опубликовано: 05.06.2016 21:35:11
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Like this Alvaro in my own words,

Mathcad up to 11 [my last version up to may 30 that PT deactivated all Single User Edition]
Mathcad XY plot is on "pica" [from recollection 8 pixels] thus the imposibility to equally size
two graphs in height/width. Smath plots on the canvas pixel. Each time it reaches a pixel it
calculates the ordinate value and plug the color, thus finally tracing the function. This porocess
is very fast, but at the kernel level and not accessible to user. To get an export trace then you
must create a vector of discrete values in the number of points as you wish.

Martin program is very crafty ! Instead of hatching by creating each vertical bar manually, it
creates the same as manual but adjustabe in 'N' and between the two plots in question. His argument
of the plot is f,g,x.1,x.2,N in hatch(f,g,x.1,x.2,N), just like asking to plot 'x' in ln(x).
So, 'f,g,x.1,x.2,N' is purely "scalar" in the sense of a vanishing iterator once done the plotted
value on the Smath QuickPlot.

As you remember from Mathsoft Collaboratory, so much I loved Mathcad 2D ...
... so much Smath QuickPlot produces more convenient graphs. OK, some missing features but monkeys
survive with bananas only, is it ? On the hatching demo [attached next post], if you plot points
the hatching looks blank. Not at all, they are all there but not joining because the points confuse
on each plot trace.

Jean
#16 Опубликовано: 05.06.2016 21:37:37
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

... the hatching demo

2DPlots Hatch Demo.sm (8 КиБ) скачан 116 раз(а).
#17 Опубликовано: 05.06.2016 22:30:46
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

... works as expected !

Forum Hatch Demo Curvature.gif

Maths Tangent.sm (18 КиБ) скачан 118 раз(а).
#18 Опубликовано: 06.06.2016 04:34:38
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: User

My versions for fill and hatch.
Have a nice day.
Alvaro.

FillRnd.sm (44 КиБ) скачан 121 раз(а).
#19 Опубликовано: 06.06.2016 09:39:37
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Only the first module works. The ones "if/else" do not in Smath 5346
The first module is fully compatible with Maxima X_Y plot, i.e:
traces color, symbols, hatching color ... great for publishing.
Martin scalar hatching is slightly faster, not to account in project.
Your module [Alvaro] is great for extracting a particular bar but
couldn't add to the plot ???

Tanks, Jean

Forum Hatch Demo Alvaro.gif


2DPlots Hatch Demo [Martin, Alvaro].sm (23 КиБ) скачан 112 раз(а).
#20 Опубликовано: 06.06.2016 17:26:19
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1675 понравились пользователям.

Группа: User

Wrote

Only the first module works. The ones "if/else" do not in Smath 5346



Well, works fine in last nightly build, but you're right, in others don't. I try to modify, but have a lot of issues. For instance, attached seems to work in last portable version, but can take a very long time for fill(phi(2), ...) , I suspect for some problem with the lele function. I'm interested in that function and rfill because rows of G are proportional to double integral of the functions f,g.

About the original fill function, it is a very nice job, but I prefer to define x range and Nx, Ny as number of points, instead of define the entire box x1,x2,y1,y2. But that's is ok for double integrals.

Best regards.

Alvaro.

Файл не найден.Файл не найден.
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений