www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Graphing a D function : possible?

reply z <z z.com> writes:
Is there a quick way of obtaining the graph of D functions like 
these?
```d
T f(T) if (isScalarType!T){}
```
or
```D
T[2] f(T, T)if (isScalarType!T){}
```
I know that there are graphing calculators already, but these 
don't support low level black magic like int <-> float 
conversions and i'm lost because there is no way to know if the 
code i write is correct without a graph or trial and error, hence 
the question.

Many thanks
Jun 01 2022
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Thursday, 2 June 2022 at 03:37:13 UTC, z wrote:
 Is there a quick way of obtaining the graph of D functions like 
 these?
 ```d
 T f(T) if (isScalarType!T){}
 ```
 or
 ```D
 T[2] f(T, T)if (isScalarType!T){}
 ```
 I know that there are graphing calculators already, but these 
 don't support low level black magic like int <-> float 
 conversions and i'm lost because there is no way to know if the 
 code i write is correct without a graph or trial and error, 
 hence the question.

 Many thanks
Probably the easiest way would be to run the function on a bunch of different inputs, and plot the input-output pairs as 2D or 3D points. Pretty much any plotting software should be able to handle a list of points.
Jun 02 2022
prev sibling parent harakim <harakim gmail.com> writes:
On Thursday, 2 June 2022 at 03:37:13 UTC, z wrote:
 Is there a quick way of obtaining the graph of D functions like 
 these?
 ```d
 T f(T) if (isScalarType!T){}
 ```
 or
 ```D
 T[2] f(T, T)if (isScalarType!T){}
 ```
 I know that there are graphing calculators already, but these 
 don't support low level black magic like int <-> float 
 conversions and i'm lost because there is no way to know if the 
 code i write is correct without a graph or trial and error, 
 hence the question.

 Many thanks
I'm not that familiar with D libraries, but I'm sure there is a graphing library. If not, I will make one soon because I need one. If you just need a graph, write a loop calling that function and print input, output to a file (csv) Then put it in google sheets or excel and make a graph. However, what *I* would do to determine if the function is correct is look at the output in the console then write unit tests.
Jun 02 2022