Re: 2D matrix operation (subtraction)

2020-02-21 Thread septc via Digitalmars-d-learn

On Friday, 21 February 2020 at 08:51:49 UTC, Andre Pany wrote:

Hi,

I have a 2D double array and I want to subtract from the first 
column a value,

is this possible with matrix operation in D?

```
void main()
{
double[][] data = [[0.0, 1.4], [1.0, 5.2], [2.0, 0.8]];

// subtract -2.0 from the first column for every value

// Expected output
// data = [[-2.0, 1.4], [-1.0, 5.2], [0.0, 0.8]];

}
```

Kind regards
André


I've recently learning Mir and so interested in
this topic. For the above purpose, I am wondering if
this Numpy-like approach is also valid:

y[ 0..$, 0 ] *= 100;

The online editor (https://run.dlang.io/) seems to give
the expected result.

// (the below code is based on the post by jmh530)

import std.stdio;
import mir.ndslice;

void main()
{
auto x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0].sliced(3, 2);
auto y = x.dup;

writeln( x );   // [[0, 1], [2, 3], [4, 5]]
writeln( y );   // [[0, 1], [2, 3], [4, 5]]

x.byDim!1.front.each!"a *= 100";

y[ 0..$, 0 ] *= 100;

writeln( x );   // [[0, 1], [200, 3], [400, 5]]
writeln( y );   // [[0, 1], [200, 3], [400, 5]]
}



Re: BetterC + startsWith

2020-02-21 Thread SrMordred via Digitalmars-d-learn
The issue is that strings aren't input ranges in betterC [1], 
due to autodecoding.


Normally you'd work around this using std.utf.byCodeUnit, but 
that's currently broken, because std.utf attempts to import 
core.exception.UnicodeException from druntime at module scope 
[2], causing any betterC program that imports std.utf to fail 
compilation.


So, for now, I think the best you can do is probably to 
copy-paste byCodeUnit into its own source file, and use that 
until std.utf is fixed.


[1] https://issues.dlang.org/show_bug.cgi?id=20139
[2] 
https://github.com/dlang/phobos/blob/7a656e09d23507d0c404dabaa2c440a45e7c753d/std/utf.d#L65


Oh right, thanks!
Its the third time that autodecoding bite me in betterC and i 
always forgot of the possibility.


Re: BetterC + startsWith

2020-02-21 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 22 February 2020 at 02:01:25 UTC, SrMordred wrote:

//-betterC
import core.stdc.stdio;
import std.algorithm;

void main(){
printf( "%d\n",startsWith("a","b") );
}

//Fails to compile with betterC, dmd/ldc2 last versions.

Any reason for not work with betterC or should i file the issue 
?
(Find this on a bindbc lib, so i think that it may have worked 
previously)


The issue is that strings aren't input ranges in betterC [1], due 
to autodecoding.


Normally you'd work around this using std.utf.byCodeUnit, but 
that's currently broken, because std.utf attempts to import 
core.exception.UnicodeException from druntime at module scope 
[2], causing any betterC program that imports std.utf to fail 
compilation.


So, for now, I think the best you can do is probably to 
copy-paste byCodeUnit into its own source file, and use that 
until std.utf is fixed.


[1] https://issues.dlang.org/show_bug.cgi?id=20139
[2] 
https://github.com/dlang/phobos/blob/7a656e09d23507d0c404dabaa2c440a45e7c753d/std/utf.d#L65


BetterC + startsWith

2020-02-21 Thread SrMordred via Digitalmars-d-learn

//-betterC
import core.stdc.stdio;
import std.algorithm;

void main(){
printf( "%d\n",startsWith("a","b") );
}

//Fails to compile with betterC, dmd/ldc2 last versions.

Any reason for not work with betterC or should i file the issue ?
(Find this on a bindbc lib, so i think that it may have worked 
previously)


Re: 2D matrix operation (subtraction)

2020-02-21 Thread jmh530 via Digitalmars-d-learn

On Friday, 21 February 2020 at 14:43:37 UTC, jmh530 wrote:

[snip]


Actually, I kind of prefer the relevant line as
x.byDim!1[0].each!"a -= 2";
which makes it a little clearer that you can easily change [0] to 
[1] to apply each to the second column instead.


Re: Conditional Attributes

2020-02-21 Thread Marcel via Digitalmars-d-learn

On Thursday, 20 February 2020 at 17:41:54 UTC, Dennis wrote:

On Tuesday, 18 February 2020 at 17:11:55 UTC, Marcel wrote:
Say I have a struct where every member function can either be 
static or not depending on a template parameter. Is there a 
simple way to do this?


The best I can think of is:

```
mixin template maybeStatic() {
void foo() {
// implementation
}
}

struct S(bool condition) {
static if (condition) {
static {
mixin maybeStatic;
}
} else {
mixin maybeStatic;
}
}
```

What do you need this for? It seems like an unusual situation 
to me.


That will do, thank you!
I'm making an allocator library similar to what Andrei 
Alexandrescu presented at CppCon. Since some allocators may not 
have state, those that inherit* from them may need to have every 
member function marked as static.


*I'm using mixins instead of inheritance.


Re: Conditional Attributes

2020-02-21 Thread Marcel via Digitalmars-d-learn
On Friday, 21 February 2020 at 01:41:21 UTC, Steven Schveighoffer 
wrote:

On 2/18/20 12:11 PM, Marcel wrote:

Hello!

Say I have a struct where every member function can either be 
static or not depending on a template parameter. Is there a 
simple way to do this? Like, for example:


struct Foo(Condition)
{
    static if (Condition) static:

    void Bar() {}
    void Baz() {}

}


Since D conditional compilation must always be a valid 
declaration or statement, you cannot do something like this.


The closest you can do is to use mixins, which means you have 
to write everything inside strings.


It has been proposed quite a few times to have a way to 
enable/disable attributes based on a compile-time boolean. But 
it's never come close to getting included.


-Steve


That's a shame...


Re: is(typeof(...)) vs __traits(compiles, ...)

2020-02-21 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/21/20 5:15 AM, drug wrote:

Currently this code does not compiles:
```
unittest
{
     class MyClass
     {
     T opCall(T)(T p)
     {
     return p;
     }
     }

     import std.container.array : Array;

     Array!MyClass arr;
}
```
but if you comment out `opCall` in MyClass this code compiles. This is 
caused by this in std.conv(4434):

```
 static if (is(typeof(chunk = T(args
     chunk = T(args);
```
The reason is that `is(typeof(chunk = T(args)))` returns true but does 
not compiles becase MyClass has `opCall`, compiler calls `opCall` but it 
needs `this` pointer that is unavailable. I replaced it by

```
 static if (__traits(compiles, chunk = T(args)))
     chunk = T(args);
```
it works but I'm not sure this good solution. The question is - 
shouldn't `typeof` return false in this case? if so then the right fix 
would be fix typeof.


This is a bug for is(typeof). It should indeed reject that call.

My understanding about __traits(compiles) is that it does some funky 
things in terms of allowing compilation that isn't normally allowed, 
which is a reason to prefer is(typeof). There are probably bugzilla 
issues on this. I remember compiler gurus (maybe Timon?) talking about 
this at one point.


I would say file an issue with a minimal test case. Any time you have:

static if(is(typeof(expr))) expr;

It should not error (excepting that there are some cases, such as 
expressions which can't technically be statements).


-Steve


Re: 2D matrix operation (subtraction)

2020-02-21 Thread jmh530 via Digitalmars-d-learn

On Friday, 21 February 2020 at 11:53:02 UTC, Ali Çehreli wrote:

[snip]
auto byColumn(R)(R range, size_t n) {
  return Column!R(range, n);
}


mir has byDim for something similar (numir also has alongDim).

This is how you would do it:

import mir.ndslice;

void main() {
auto x = [0.0, 1.4, 1.0, 5.2, 2.0, 0.8].sliced(3, 2);
x.byDim!1.front.each!"a -= 2";
}

My recollection is that it is a little bit trickier if you want 
to subtract a vector from each column of a matrix (the sweep 
function in R).


Re: 2D matrix operation (subtraction)

2020-02-21 Thread Andre Pany via Digitalmars-d-learn

On Friday, 21 February 2020 at 11:53:02 UTC, Ali Çehreli wrote:

On 2/21/20 12:51 AM, Andre Pany wrote:

Hi,

I have a 2D double array and I want to subtract from the first 
column a value,

is this possible with matrix operation in D?

```
void main()
{
     double[][] data = [[0.0, 1.4], [1.0, 5.2], [2.0, 0.8]];

     // subtract -2.0 from the first column for every value

     // Expected output
     // data = [[-2.0, 1.4], [-1.0, 5.2], [0.0, 0.8]];

}
```

Kind regards
André





I don't have experience with it but mir's ndslice is designed 
for this:


  https://github.com/libmir/mir-algorithm

Although it feels like something similar is probably already in 
Phobos, I've come up with the following solution just now for 
fun:


import std.stdio;
import std.algorithm;
import std.range;

// At least something similar to this exists in Phobos?
struct ElementReference(R) {
  ElementType!(ElementType!R) * p;

  ref reference() {
return *p;
  }

  alias reference this;
}

struct Column(R) {
  R range;
  size_t n;

  auto empty() {
return range.empty;
  }

  auto front() {
return ElementReference!R(&(range.front[n]));
  }

  auto popFront() {
return range.popFront();
  }
}

auto byColumn(R)(R range, size_t n) {
  return Column!R(range, n);
}

void main() {
  double[][] data = [[0.0, 1.4], [1.0, 5.2], [2.0, 0.8]];

  data.byColumn(0).each!(a => a -= 2.0);
  writeln(data);
}

Ali


Thanks a lot. Mir is great and actually I try to rewrite some 
Python
Pandas Dataframe index logic. For my current project any 
dependency less
is a little headache less, therefore I try to avoid Mir at the 
moment,

but will definitely will have a look whether I can use it.

Thanks for the example, I will use it.

Kind regards
André




Re: GtkD crash

2020-02-21 Thread mark via Digitalmars-d-learn
Thanks for your question, it led me to focus on the Label and now 
I've solved the problem.


I thought that onChangeState was never called before the Label 
was constructed, but it turns out it is called before. So now I 
use:


if (statusLabel !is null)
statusLabel.setText(message);

Now it works.

Thanks!


Re: 2D matrix operation (subtraction)

2020-02-21 Thread Ali Çehreli via Digitalmars-d-learn

On 2/21/20 12:51 AM, Andre Pany wrote:

Hi,

I have a 2D double array and I want to subtract from the first column a 
value,

is this possible with matrix operation in D?

```
void main()
{
     double[][] data = [[0.0, 1.4], [1.0, 5.2], [2.0, 0.8]];

     // subtract -2.0 from the first column for every value

     // Expected output
     // data = [[-2.0, 1.4], [-1.0, 5.2], [0.0, 0.8]];

}
```

Kind regards
André





I don't have experience with it but mir's ndslice is designed for this:

  https://github.com/libmir/mir-algorithm

Although it feels like something similar is probably already in Phobos, 
I've come up with the following solution just now for fun:


import std.stdio;
import std.algorithm;
import std.range;

// At least something similar to this exists in Phobos?
struct ElementReference(R) {
  ElementType!(ElementType!R) * p;

  ref reference() {
return *p;
  }

  alias reference this;
}

struct Column(R) {
  R range;
  size_t n;

  auto empty() {
return range.empty;
  }

  auto front() {
return ElementReference!R(&(range.front[n]));
  }

  auto popFront() {
return range.popFront();
  }
}

auto byColumn(R)(R range, size_t n) {
  return Column!R(range, n);
}

void main() {
  double[][] data = [[0.0, 1.4], [1.0, 5.2], [2.0, 0.8]];

  data.byColumn(0).each!(a => a -= 2.0);
  writeln(data);
}

Ali



Re: betterC CTFE nested switch

2020-02-21 Thread Stefan Koch via Digitalmars-d-learn

On Friday, 21 February 2020 at 09:03:26 UTC, Abby wrote:

On Monday, 17 February 2020 at 19:02:50 UTC, Stefan Koch wrote:

Sorry I just realized I never published the code.
I am going to add it to ctfeutils.


Hi Stefan,
I'm sorry to bother you, I just wanted to kindly ask if you 
would upload the formatter to ctfeutils on github it would help 
me alot.


Thank you very much
Kind regards Abby


No problem I am a little busy lately.

In the meantime you can check if std.format : format would do the 
job.
Even though it will like be much slower than my CTFE optimized 
version.


is(typeof(...)) vs __traits(compiles, ...)

2020-02-21 Thread drug via Digitalmars-d-learn

Currently this code does not compiles:
```
unittest
{
class MyClass
{
T opCall(T)(T p)
{
return p;
}
}

import std.container.array : Array;

Array!MyClass arr;
}
```
but if you comment out `opCall` in MyClass this code compiles. This is 
caused by this in std.conv(4434):

```
static if (is(typeof(chunk = T(args
chunk = T(args);
```
The reason is that `is(typeof(chunk = T(args)))` returns true but does 
not compiles becase MyClass has `opCall`, compiler calls `opCall` but it 
needs `this` pointer that is unavailable. I replaced it by

```
static if (__traits(compiles, chunk = T(args)))
chunk = T(args);
```
it works but I'm not sure this good solution. The question is - 
shouldn't `typeof` return false in this case? if so then the right fix 
would be fix typeof.


Re: State of MIPS

2020-02-21 Thread April via Digitalmars-d-learn

Thanks all, much appreciated!


Re: GtkD crash

2020-02-21 Thread Ron Tarrant via Digitalmars-d-learn

On Friday, 21 February 2020 at 08:55:43 UTC, mark wrote:


statusLabel.setText(message); // BUG


Where is statusLabel instantiated?

Other than that, I see nothing wrong here.



Re: betterC CTFE nested switch

2020-02-21 Thread Abby via Digitalmars-d-learn

On Monday, 17 February 2020 at 19:02:50 UTC, Stefan Koch wrote:

Sorry I just realized I never published the code.
I am going to add it to ctfeutils.


Hi Stefan,
I'm sorry to bother you, I just wanted to kindly ask if you would 
upload the formatter to ctfeutils on github it would help me alot.


Thank you very much
Kind regards Abby


GtkD crash

2020-02-21 Thread mark via Digitalmars-d-learn
I'm porting a simple game to GtkD to learn the library and more 
about D.


Unfortunately, I've hit a show-stopping crash.

I have a subclass of ApplicationWindow which has this method:

private void onChangeState(int score, Board.State state) {
import std.format: format;

string message;
if (state == Board.State.GAME_OVER)
message = format("%,d Game Over", score);
else if (state == Board.State.USER_WON) {
if (score > highScore) {
message = format("%,d New High Score!", score);
highScore = score; // TODO save highScore
} else
message = format("%,d You Won!", score);
} else // still playing
message = format("%,d/%,d", score, highScore);
statusLabel.setText(message); // BUG
}

This method gets passed to another widget which calls it whenever 
the game's state or score changes.


If the BUG line is commented out, the program runs fine (well, 
except that the Label always shows "0/0").


But if the BUG line is uncommented (as above), it crashes:

$ dub
Performing "debug" build using 
/home/mark/opt/ldc2-1.20.0-linux-x86_64/bin/ldc2 for x86_64.
gtk-d:gtkd 3.9.0: target for configuration "library" is up to 
date.
gravitate ~master: target for configuration "application" is up 
to date.

To force a rebuild of up-to-date targets, run again with --force.
Running ./gravitate
onChangeState 0 PLAYING
Program exited with code -11

Here's what it looks like in gdb (slightly edited):

$ gdb gravitate
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
[snip]
This GDB was configured as "x86_64-linux-gnu".
[snip]
Reading symbols from gravitate...done.
(gdb) run
Starting program: /home/mark/app/gravitate/d/gravitate
[Thread debugging using libthread_db enabled]
Using host libthread_db library 
"/lib/x86_64-linux-gnu/libthread_db.so.1".

[New Thread 0x76e02700 (LWP 7017)]
[New Thread 0x76601700 (LWP 7018)]
[New Thread 0x75e00700 (LWP 7019)]
[New Thread 0x755ff700 (LWP 7020)]
[New Thread 0x74dfe700 (LWP 7021)]
[New Thread 0x7fffd700 (LWP 7022)]
[New Thread 0x7fffdf7fe700 (LWP 7023)]
[New Thread 0x7fffce3a5700 (LWP 7024)]
[New Thread 0x7fffcdba4700 (LWP 7025)]
onChangeState 0 PLAYING

Thread 1 "gravitate" received signal SIGSEGV, Segmentation fault.
0x55704cc1 in gamewindow.GameWindow.onChangeState(int, 
board.Board.State) (this=0x77ecf700, score=0,

state=board.Board.PLAYING) at gamewindow.d:182
182 statusLabel.setText(message);
(gdb) bt
warning: (Internal error: pc 0x55703fbf in read in psymtab, 
but not in symtab.)
warning: (Internal error: pc 0x55703fbf in read in psymtab, 
but not in symtab.)

[ + several more like these ]

#0  0x55704cc1 in 
gamewindow.GameWindow.onChangeState(int, board.Board.State) 
(this=0x77ecf700, score=0, state=board.Board.PLAYING) at 
gamewindow.d:182
warning: (Internal error: pc 0x55703fbf in read in psymtab, 
but not in symtab.)
#1  0x55728246 in board.Board.newGame() 
(this=0x7fffcc92d000) at board.d:60
warning: (Internal error: pc 0x55703fbf in read in psymtab, 
but not in symtab.)
warning: (Internal error: pc 0x55703a2b in read in psymtab, 
but not in symtab.)
#2  0x55704bb8 in 
_D5board5Board6__ctorMFDFiEQzQv5StateZvZCQBnQBk (warning: 
(Internal error: pc 0x55703fbf in read in psymtab, but not in 
symtab.)
this=0x7fffcc92d000, warning: (Internal error: pc 0x55703fbf 
in read in psymtab, but not in symtab.)

onChangeState=...)
at board.d:42
warning: (Internal error: pc 0x55703fbf in read in psymtab, 
but not in symtab.)
warning: (Internal error: pc 0x55703a2b in read in psymtab, 
but not in symtab.)
#3  0x55703fc0 in warning: (Internal error: pc 
0x55703fbf in read in psymtab, but not in symtab.)
gamewindow.GameWindow.makeWidgets()warning: (Internal error: pc 
0x55703fbf in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x55703fbf in read in psymtab, 
but not in symtab.)
 (warning: (Internal error: pc 0x55703fbf in read in psymtab, 
but not in symtab.)
warning: (Internal error: pc 0x55703a2b in read in psymtab, 
but not in symtab.)
this=0x77ecf700)warning: (Internal error: pc 0x55703fbf 
in read in psymtab, but not in symtab.)
 at gamewindow.dwarning: (Internal error: pc 0x55703fbf in 
read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x55703fbf in read in psymtab, 
but not in symtab.)

:58
warning: (Internal error: pc 0x55703a2b in read in psymtab, 
but not in symtab.)
#4  0x55703a2c in warning: (Internal error: pc 
0x55703a2b in read in psymtab, but not in symtab.)

_D10gamewindow10GameWindow6__ctorMFC3gtk11ApplicationQnZCQCdQBuwarning: 
(Internal error: pc 0x55703a2b in read in psymtab, but not in symtab.)
warning: (Internal error: pc 0x55703a2b in read in psymtab, 
but not in symtab.)
 (warning: (Internal error: pc 

2D matrix operation (subtraction)

2020-02-21 Thread Andre Pany via Digitalmars-d-learn

Hi,

I have a 2D double array and I want to subtract from the first 
column a value,

is this possible with matrix operation in D?

```
void main()
{
double[][] data = [[0.0, 1.4], [1.0, 5.2], [2.0, 0.8]];

// subtract -2.0 from the first column for every value

// Expected output
// data = [[-2.0, 1.4], [-1.0, 5.2], [0.0, 0.8]];

}
```

Kind regards
André