Re: D and math, can you isolate this ?

2016-09-20 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 20 September 2016 at 12:35:18 UTC, Basile B. wrote:
I've recently started an easing/interpolation family of 
function in my D user library. It's based on something I know 
well since I've already used them in 2012 in a VST plugin 
called GrainPlot (RIP).


However for one of the function, I can't manage to get the 
inverse.


A function that's fully implemented:
https://github.com/BBasile/iz/blob/master/import/iz/math.d#L598
- f(x,c) = x*x*x - x*x*c + x*c;
- c(f(0.5)) = 4 * (y - 0.125));

Another:
https://github.com/BBasile/iz/blob/master/import/iz/math.d#L749
- f(x,c) = pow(x, c);
- c(f(0.5)) = log(y) / log(0.5));

The problem is here:
https://github.com/BBasile/iz/blob/master/import/iz/math.d#L849
- f(x,c) = 1.0 - pow(1.0 - pow(x, 2.0/c), c * 0.5);
- c(f0.5)) = ?

Which means that I ask you if you can isolate c for

y = 1.0 - pow(1.0 - pow(0.5, 2.0/c), c * 0.5);

y is always f(0.5,c)


So if we rearrange and take the logs of both sides and divide by 
c we get


2*log(1-y)/c = log(1-2^(-2/c))

and then that we have one occurrence of c on each side do an 
iterative back substitution to find the intersection given that 
you know for y=0.5 ,c = 2.
We used this method for finding voltages and currents in circuits 
with semiconductors.


Re: What exactly does the compiler switch -betterC do?

2016-09-20 Thread Anonymouse via Digitalmars-d-learn

On Monday, 20 June 2016 at 06:35:32 UTC, Jacob Carlborg wrote:

On 2016-06-19 21:53, Gary Willoughby wrote:
When compiling, what exactly does the -betterC flag do? The 
command help
says "omit generating some runtime information and helper 
functions" but

what does this really mean? Is there any specifics somewhere?


It is intended to allow you to link an application without 
druntime. [...]


What is the equavilent in gdc and ldc?




Re: Append const to array

2016-09-20 Thread Yuxuan Shui via Digitalmars-d-learn
On Tuesday, 20 September 2016 at 22:38:33 UTC, Jonathan M Davis 
wrote:
On Tuesday, September 20, 2016 22:23:08 Yuxuan Shui via 
Digitalmars-d-learn wrote:

struct A {
  ulong[] x;
}
struct B {
  ulong x;
}
void main() {
  B[] b;
  const(B) xx = B(1);
  b ~= xx; // Works

  A[] c;
  const(A) yy = A([1]);
  c ~= yy; // Does not
}

What gives?


const(A) means that the ulong[] inside is const(ulong[]). When 
yy is copied
to be appended to c, it goes from const(A) to A, which means 
that
const(ulong[]) would need to be sliced and and set to ulong[], 
which would
violate const, because it would mean that the last element in c 
could mutate
then elements of its x, which would then mutate the elements in 
yy.


- Jonathan M Davis


That makes sense, thanks.


Re: Append const to array

2016-09-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, September 20, 2016 22:23:08 Yuxuan Shui via Digitalmars-d-learn 
wrote:
> struct A {
>   ulong[] x;
> }
> struct B {
>   ulong x;
> }
> void main() {
>   B[] b;
>   const(B) xx = B(1);
>   b ~= xx; // Works
>
>   A[] c;
>   const(A) yy = A([1]);
>   c ~= yy; // Does not
> }
>
> What gives?

const(A) means that the ulong[] inside is const(ulong[]). When yy is copied
to be appended to c, it goes from const(A) to A, which means that
const(ulong[]) would need to be sliced and and set to ulong[], which would
violate const, because it would mean that the last element in c could mutate
then elements of its x, which would then mutate the elements in yy.

- Jonathan M Davis



Append const to array

2016-09-20 Thread Yuxuan Shui via Digitalmars-d-learn


struct A {
ulong[] x;
}
struct B {
ulong x;
}
void main() {
B[] b;
const(B) xx = B(1);
b ~= xx; // Works

A[] c;
const(A) yy = A([1]);
c ~= yy; // Does not
}

What gives?


Re: Using Libraries

2016-09-20 Thread Karabuta via Digitalmars-d-learn

On Tuesday, 20 September 2016 at 15:38:55 UTC, Darren wrote:
On Tuesday, 20 September 2016 at 15:07:53 UTC, rikki cattermole 
wrote:



Ok lets start at the very beginning...


I think I need to start before that, haha.

I might need more of a step-by-step guide.  I'm a complete 
beginner to programming, not just D.  I worked through 
Programming in D, where I was just compiling with dmd, then 
when I decided to learn OpenGL I seem to be using dub for 
everything.


There have been a few libraries I've wanted to use but couldn't 
because they didn't have a pre-compiled binary, which is all 
I've been able to get working through sheer trial and error.  
Some sites say to use things like CMake and cygwin, but I'm 
uncomfortable using things I have no idea about.


Dub is like a package manager for D (like what npm is to 
node.js). All dub libraries are hosted at code.dlang.org. When 
you see a library at code.dlang.org you want to use, you could 
either type "dub install packagename" whilst in the dub project 
ROOT or specify dependencies in the dub.json file.
You can then run "dub run" which will take care of fetching and 
building dependencies/libraries from code.dlang.org (including 
linking and running the binary).


For example, there is a web framework called vibe.d. If I want to 
use vide.d, I can specify dependencies as;


dependencies: {
"vide-d":"^0.7.29"
}


In my app.d file (which is available for any dub project created 
using "dub init projectname") I can import vibe.d using;


import vide.d;
void main() {
...
}

I can now compile and run the program with "dub run" or "dub 
build" to only build and link without running.




setting fields of object using traits

2016-09-20 Thread Ram_B via Digitalmars-d-learn
I'm trying to set fields of object from JSON with traits library. 
How i can to it properly?


import std.stdio;
import std.json;
import std.traits;
import std.meta:  Alias;

class Obj{
void fromJSON(this T)(JSONValue j){
foreach(field; FieldNameTuple!T){
alias member = Alias!(__traits(getMember, T, field));
static if (__traits(hasMember, member, "fromJSON")){
member.fromJSON(j[field]);
} else {
member = j[field];
}
}
}
}

class A : Obj{
int a,b;
C c;
this(){
c = new C();
}

}

class C : Obj{
int a;
this(){
a = 0;
};
}

int main(string[] argv)
{
string s = "{\"a\": 1, \"b\": 2, \"c\": {\"a\": 3} }";
JSONValue j = parseJSON(s);
A a = new A();
a.fromJSON(j);
writeln(a.b);
readln();
return 0;
}

main.d(14): Error: need 'this' for 'a' of type 'int'
main.d(14): Error: need 'this' for 'b' of type 'int'
main.d(12): Error: template main.Obj.fromJSON cannot deduce 
function from argument types !(c)(JSONValue), candidates are:

main.d(8):main.Obj.fromJSON(this T)(JSONValue j)
main.d(41): Error: template instance main.Obj.fromJSON!(A) error 
instantiating




Re: thisExePath purity

2016-09-20 Thread crimaniak via Digitalmars-d-learn
On Tuesday, 20 September 2016 at 13:35:27 UTC, Steven 
Schveighoffer wrote:
Yes, but if your code does instantiate it, it is called, even 
if you don't ever call the function that calls it.
Yes, it's not ideal but better then just global variable and 
static block - it's called in any case, even if variable is not 
used at all.


Ideal solution will be something like attribute for static block 
leading to make it optional, so module will not be included if no 
usage of other symbols found. But I don't know way how to make it 
so template is used.


Note that if you don't import the module that contains the 
static ctor, it should be trimmed by the linker.
 Let's imagine linker can trim even imported module with static 
ctor, if we have something like:


immutable string executablePath;

@local shared static this()
{
import std.file : thisExePath;
executablePath = thisExePath();
}

and there is no references to executablePath. Here it would be 
useful, I think. Attribute @local (or @module? the name does not 
matter) mean this block used only to init other symbols in this 
module so it can be skipped if no references.



I would absolutely caution you from putting static this() 
inside any template. Unfortunately, due to the way D generates 
these static constructors, any module that uses staticMemoize, 
or *imports a module that uses it*, will be marked as having a 
static constructor, and will potentially create cycles.
 Please be more detail about cycles. Do you mean something like 
this? https://isocpp.org/wiki/faq/ctors#static-init-order




Re: thisExePath purity

2016-09-20 Thread crimaniak via Digitalmars-d-learn

On Tuesday, 20 September 2016 at 09:14:39 UTC, Marc Schütz wrote:

Have a look at `std.concurrency.initOnce`:
https://dlang.org/phobos/std_concurrency.html#.initOnce

But you will still need to use assumePure() for calling 
`thisExePath`, and it might do other things that are impure...


Yes, it's near but in this case I try to fix purity, so any 
variants of lazy initialization is not applicable here.


Re: thisExePath purity

2016-09-20 Thread crimaniak via Digitalmars-d-learn
On Tuesday, 20 September 2016 at 04:26:05 UTC, Jonathan M Davis 
wrote:
On Tuesday, September 20, 2016 04:17:21 crimaniak via 
Digitalmars-d-learn wrote:

 static shared immutable ReturnType!T value;


I would point out that immutable is implicitly shared, so 
there's no reason to put shared on an immutable variable. 
However, you _do_ want to put shared on a static constructor 
that initializes an immutable variable so that it's only run 
once for the program instead of once per thread (the compiler 
really should enforce that, but there's a longstanding bug that 
allows you to reinitialize an immutable variable by not putting 
shared on the static constructor and starting multiple threads).

 Ok, I got it. Thanks.


Re: D and math, can you isolate this ?

2016-09-20 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Sep 20, 2016 at 09:22:19AM -0700, H. S. Teoh via Digitalmars-d-learn 
wrote:
> On Tue, Sep 20, 2016 at 12:35:18PM +, Basile B. via Digitalmars-d-learn 
> wrote:
> [...]
[...]
> > Which means that I ask you if you can isolate c for
> > 
> > y = 1.0 - pow(1.0 - pow(0.5, 2.0/c), c * 0.5);
> > 
> > y is always f(0.5,c)
[...]
> That probably means the inverse cannot be expressed in terms of
> elementary functions. Probably the only thing you can do is to use
> some kind of numerical approximation, like some form of Newton's
> method or some such, to find the value of c.
[...]

It may be analytically very hard to solve this equation, but it's
probably not so hard to solve numerically. Based on the graph of the
equation produced by Wolfram Alpha, it seems that y must always lie
between 0 and 1, and that it has a horizontal asymptote at y=1.  At
around c=6 or thereabouts, y becomes very close to 1.  The value of c
for y=0.5 is approximately 2, so that seems like a good initial guess
for an iterative method.

So if y<0 or y>1, return NaN. If y=1, return +inf. Otherwise, use an
iterative method with a starting value of c=2. Because of the horizontal
asymptote at y=1, though, values of c much greater than 6 will probably
be quite inaccurate, so hopefully your application doesn't depend on the
exact value in that case!


T

-- 
Freedom of speech: the whole world has no right *not* to hear my spouting off!


Re: D and math, can you isolate this ?

2016-09-20 Thread Basile B. via Digitalmars-d-learn

On Tuesday, 20 September 2016 at 16:22:19 UTC, H. S. Teoh wrote:
On Tue, Sep 20, 2016 at 12:35:18PM +, Basile B. via 
Digitalmars-d-learn wrote: [...]

The problem is here:
https://github.com/BBasile/iz/blob/master/import/iz/math.d#L849
- f(x,c) = 1.0 - pow(1.0 - pow(x, 2.0/c), c * 0.5);
- c(f0.5)) = ?

Which means that I ask you if you can isolate c for

y = 1.0 - pow(1.0 - pow(0.5, 2.0/c), c * 0.5);

y is always f(0.5,c)


I couldn't manage to solve it.  Nested exponentials are very 
nasty to invert. :-(  At first, I thought it might be solvable 
in terms of the Lambert W function (aka ProductLog) but I 
couldn't manage to get the equation into the right form.  Then 
I checked on Wolfram Alpha and it says "no result found in 
terms of standard mathematical functions".


That probably means the inverse cannot be expressed in terms of 
elementary functions. Probably the only thing you can do is to 
use some kind of numerical approximation, like some form of 
Newton's method or some such, to find the value of c.



T


Thanks for trying, you're not the first to tell me about the 
Newton's method...


Re: D and math, can you isolate this ?

2016-09-20 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Sep 20, 2016 at 12:35:18PM +, Basile B. via Digitalmars-d-learn 
wrote:
[...]
> The problem is here:
> https://github.com/BBasile/iz/blob/master/import/iz/math.d#L849
> - f(x,c) = 1.0 - pow(1.0 - pow(x, 2.0/c), c * 0.5);
> - c(f0.5)) = ?
> 
> Which means that I ask you if you can isolate c for
> 
> y = 1.0 - pow(1.0 - pow(0.5, 2.0/c), c * 0.5);
> 
> y is always f(0.5,c)

I couldn't manage to solve it.  Nested exponentials are very nasty to
invert. :-(  At first, I thought it might be solvable in terms of the
Lambert W function (aka ProductLog) but I couldn't manage to get the
equation into the right form.  Then I checked on Wolfram Alpha and it
says "no result found in terms of standard mathematical functions".

That probably means the inverse cannot be expressed in terms of
elementary functions. Probably the only thing you can do is to use some
kind of numerical approximation, like some form of Newton's method or
some such, to find the value of c.


T

-- 
Questions are the beginning of intelligence, but the fear of God is the 
beginning of wisdom.


Re: Using Libraries

2016-09-20 Thread Darren via Digitalmars-d-learn
On Tuesday, 20 September 2016 at 15:07:53 UTC, rikki cattermole 
wrote:



Ok lets start at the very beginning...


I think I need to start before that, haha.

I might need more of a step-by-step guide.  I'm a complete 
beginner to programming, not just D.  I worked through 
Programming in D, where I was just compiling with dmd, then when 
I decided to learn OpenGL I seem to be using dub for everything.


There have been a few libraries I've wanted to use but couldn't 
because they didn't have a pre-compiled binary, which is all I've 
been able to get working through sheer trial and error.  Some 
sites say to use things like CMake and cygwin, but I'm 
uncomfortable using things I have no idea about.


Re: Using Libraries

2016-09-20 Thread rikki cattermole via Digitalmars-d-learn

On 21/09/2016 3:01 AM, Darren wrote:

Hey, all

I keep hitting roadblocks and that's mainly due to not knowing how to
include libraries.  So far I've been getting by with downloading .dll's
and including the necessary dependencies in the dub.json file and having
that build/run my project.  I'm sure I'm making a mess of that, too, but
it works and now I need to learn how to include static libraries (and
probably understand github and other dub features).

Right now, for example, I want to use the gl3n package:
https://github.com/Dav1dde/gl3n

What do I need in order to build libraries, and have dub include them
when I import modules?   Can I keep all of the libraries in one place so
I'm not copy-pasting them (like in lib and bin folders that I keep seeing)?

As you can tell, I'm still very new to all of this and I have no idea
where to start.  Thank you for your time!


Ok lets start at the very beginning with straight dmd.

You have a total of two things you can pass in, an import directory and 
source files.
Source files are compiled in and import directories are basically a way 
to tell the compiler that certain symbols can exist but it won't create 
them.


Now from this you abstract away into dependencies such as packages / 
subpackages.
This is where dub comes in, it will fetch (known) projects with dub 
definition files (dub.json/sdl), place them into a folder under your 
profile directory and allow you to build against them and automatically 
provide them as import directories as required.


So how do you do that? Simple.

"dependencies": {
"mypackage": ">=0.0.0"
}

Inside of a dub.json file (sdl is a little different check 
code.dlang.org for more help on the subject).


Using Libraries

2016-09-20 Thread Darren via Digitalmars-d-learn

Hey, all

I keep hitting roadblocks and that's mainly due to not knowing 
how to include libraries.  So far I've been getting by with 
downloading .dll's and including the necessary dependencies in 
the dub.json file and having that build/run my project.  I'm sure 
I'm making a mess of that, too, but it works and now I need to 
learn how to include static libraries (and probably understand 
github and other dub features).


Right now, for example, I want to use the gl3n package: 
https://github.com/Dav1dde/gl3n


What do I need in order to build libraries, and have dub include 
them when I import modules?   Can I keep all of the libraries in 
one place so I'm not copy-pasting them (like in lib and bin 
folders that I keep seeing)?


As you can tell, I'm still very new to all of this and I have no 
idea where to start.  Thank you for your time!


Re: D and math, can you isolate this ?

2016-09-20 Thread Basile B. via Digitalmars-d-learn

On Tuesday, 20 September 2016 at 12:35:18 UTC, Basile B. wrote:
I've recently started an easing/interpolation family of 
function in my D user library. It's based on something I know 
well since I've already used them in 2012 in a VST plugin 
called GrainPlot (RIP).


However for one of the function, I can't manage to get the 
inverse.

[...]
The problem is here:
https://github.com/BBasile/iz/blob/master/import/iz/math.d#L849
- f(x,c) = 1.0 - pow(1.0 - pow(x, 2.0/c), c * 0.5);
- c(f0.5)) = ?

Which means that I ask you if you can isolate c for

y = 1.0 - pow(1.0 - pow(0.5, 2.0/c), c * 0.5);

y is always f(0.5,c)


If you don't understand, these function have a control point, for 
"parabol" and "pow" it's easy to get the c Coefficient that 
manages the slope. But for the ellipse (aka the super ellipse) 
it's a math nightmare )


For example is use the three functions in the same order 
(parabol, pow, ellipse):


http://sendvid.com/ygti5jmr

for the ellipse you can see that the mouse position is not in 
sync with the control point at the middle...it's the problem.


I need to isolate c when "y = 1.0 - pow(1.0 - pow(0.5, 2.0/c), c 
* 0.5)".

I know it's hard...otherwise I wouldn't ask ;]




Re: thisExePath purity

2016-09-20 Thread Steven Schveighoffer via Digitalmars-d-learn

On 9/20/16 12:17 AM, crimaniak wrote:

Hi and thanks all!

On Tuesday, 20 September 2016 at 00:43:10 UTC, Jonathan M Davis wrote:


immutable string executablePath;

shared static this()
{
import std.file : thisExePath();
executablePath = thisExePath();
}


This code is good for my needs but I start to think about how to call
thisExePath only if it is really used and come to this solution:

import std.traits: ReturnType, Parameters;

string staticMemoize(alias T, Parms = Parameters!T)() pure
{
struct Holder(alias T)
{
static shared immutable ReturnType!T value;
shared static this(){ value = T(Parms); }
}

return Holder!T.value;
}

unittest
{
import std.file : thisExePath;
assert(staticMemoize!thisExePath == thisExePath);
}

Something like this. Need to refine about input parameters, but I hope,
idea is clear.
Unlike the function memoize from phobos staticMemoize really pure. And
unlike proposed solution with ordinary variable staticMemoize is lazy,
because no call - no instantiation.


Yes, but if your code does instantiate it, it is called, even if you 
don't ever call the function that calls it.


Note that if you don't import the module that contains the static ctor, 
it should be trimmed by the linker.


I would absolutely caution you from putting static this() inside any 
template. Unfortunately, due to the way D generates these static 
constructors, any module that uses staticMemoize, or *imports a module 
that uses it*, will be marked as having a static constructor, and will 
potentially create cycles.


-Steve


D and math, can you isolate this ?

2016-09-20 Thread Basile B. via Digitalmars-d-learn
I've recently started an easing/interpolation family of function 
in my D user library. It's based on something I know well since 
I've already used them in 2012 in a VST plugin called GrainPlot 
(RIP).


However for one of the function, I can't manage to get the 
inverse.


A function that's fully implemented:
https://github.com/BBasile/iz/blob/master/import/iz/math.d#L598
- f(x,c) = x*x*x - x*x*c + x*c;
- c(f(0.5)) = 4 * (y - 0.125));

Another:
https://github.com/BBasile/iz/blob/master/import/iz/math.d#L749
- f(x,c) = pow(x, c);
- c(f(0.5)) = log(y) / log(0.5));

The problem is here:
https://github.com/BBasile/iz/blob/master/import/iz/math.d#L849
- f(x,c) = 1.0 - pow(1.0 - pow(x, 2.0/c), c * 0.5);
- c(f0.5)) = ?

Which means that I ask you if you can isolate c for

y = 1.0 - pow(1.0 - pow(0.5, 2.0/c), c * 0.5);

y is always f(0.5,c)


Re: thisExePath purity

2016-09-20 Thread Marc Schütz via Digitalmars-d-learn

On Tuesday, 20 September 2016 at 04:17:21 UTC, crimaniak wrote:

Hi and thanks all!

On Tuesday, 20 September 2016 at 00:43:10 UTC, Jonathan M Davis 
wrote:



immutable string executablePath;

shared static this()
{
import std.file : thisExePath();
executablePath = thisExePath();
}


This code is good for my needs but I start to think about how 
to call thisExePath only if it is really used and come to this 
solution:


import std.traits: ReturnType, Parameters;

string staticMemoize(alias T, Parms = Parameters!T)() pure
{
struct Holder(alias T)
{
static shared immutable ReturnType!T value;
shared static this(){ value = T(Parms); }
}

return Holder!T.value;
}

unittest
{
import std.file : thisExePath;
assert(staticMemoize!thisExePath == thisExePath);
}

Something like this. Need to refine about input parameters, but 
I hope, idea is clear.
Unlike the function memoize from phobos staticMemoize really 
pure. And unlike proposed solution with ordinary variable 
staticMemoize is lazy, because no call - no instantiation.


Have a look at `std.concurrency.initOnce`:
https://dlang.org/phobos/std_concurrency.html#.initOnce

But you will still need to use assumePure() for calling 
`thisExePath`, and it might do other things that are impure...


Re: What blogs about D do you read?

2016-09-20 Thread sarn via Digitalmars-d-learn

Don't forget the Planet D aggregator :)
http://planet.dsource.org/

Here's my contribution:
https://theartofmachinery.com/tags/dlang/


Re: What blogs about D do you read?

2016-09-20 Thread Saurabh Das via Digitalmars-d-learn

On Monday, 19 September 2016 at 19:36:22 UTC, Karabuta wrote:

On Monday, 19 September 2016 at 19:29:25 UTC, A D dev wrote:

On Monday, 19 September 2016 at 17:42:51 UTC, A D dev wrote:

Hi list,

What blogs about D do you read?


To be more clear:

- what blogs that include posts on D, would you recommend to a 
D beginner?


Thanks.


I have one here on Vibe.d for beginners 
https://laberba.github.io/2016/hello-world-app-with-the-vibe.d-web-framework/


I will be writing more for-beginners blogs in the coming few 
weeks.


You blog looks gorgeous. Absolutely beautiful!