Re: Linear array to matrix

2020-04-04 Thread Giovanni Di Maria via Digitalmars-d-learn

On Saturday, 4 April 2020 at 14:00:01 UTC, 9il wrote:
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria 
wrote:

[...]


You may want to look into a mir-algorithm package that supports 
rectangular multidimensional arrays like NumPy.


[...]





Very good.
Thank you
G


Re: Linear array to matrix

2020-04-04 Thread Giovanni Di Maria via Digitalmars-d-learn
On Saturday, 4 April 2020 at 10:52:30 UTC, MoonlightSentinel 
wrote:
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria 
wrote:
Is there a Built-in function (no code, only a built-in 
function)

that transform a linear array to a Matrix?


You can combine slide [1] and array [2]:

import std;

void main()
{
auto input = [10,20,30,40,50,60,70,80,90,100,110,120];
auto output = input.slide(3, 3).array;

writeln(input);
writeln();
writeln(output);
}

Note that output is a view of input and not a copy.

[1] https://dlang.org/phobos/std_range.html#.slide
[2] https://dlang.org/phobos/std_array.html#.array




Thank you very much.
Giovanni



Re: Linear array to matrix

2020-04-04 Thread Giovanni Di Maria via Digitalmars-d-learn

On Saturday, 4 April 2020 at 10:52:00 UTC, Boris Carvajal wrote:
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria 
wrote:

Hi.
Is there a Built-in function (no code, only a built-in 
function)

that transform a linear array to a Matrix?

For example:

From

[10,20,30,40,50,60,70,80,90,100,110,120];


To

[
[10,20,30],
[40,50,60],
[70,80,90],
[100,110,120]
];

Thank You very much
Cheers.
Giovanni


If you're really sure about the array and matrix 
dimensions/types.

You can use a cast:

int[] a = [10,20,30,40,50,60,70,80,90,100,110,120];

    int[3][] m1 = cast(int[3][]) a;
    writeln(m1);

A better way is using the function chunks;

import std.range;

    auto m2 = a.chunks(3);
    writeln(m2);




Ok. Thank you for your help
Giovanni



Re: Simple array problem

2020-04-04 Thread Giovanni Di Maria via Digitalmars-d-learn
On Saturday, 4 April 2020 at 10:57:36 UTC, MoonlightSentinel 
wrote:
On Saturday, 4 April 2020 at 09:09:44 UTC, Giovanni Di Maria 
wrote:

Why the followin code gives me the error?

 Error: only one index allowed to index `int[3][3]`


Use matrix[2][2] instead of matrix[2,2].





OK.
Thank you very MUCH
Giovanni



Linear array to matrix

2020-04-04 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi.
Is there a Built-in function (no code, only a built-in function)
that transform a linear array to a Matrix?

For example:

From

[10,20,30,40,50,60,70,80,90,100,110,120];


To

[
[10,20,30],
[40,50,60],
[70,80,90],
[100,110,120]
];

Thank You very much
Cheers.
Giovanni



Simple array problem

2020-04-04 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi.
I am very new to D Language and I apologize for my bad question.

Why the followin code gives me the error?

 Error: only one index allowed to index `int[3][3]`

import std.stdio;
import std.array;
void main()
{
int[3][3] matrix;
matrix[2,2]=99;
}


Thank you very much
GIovanni


Re: Slow UDF call?

2019-08-17 Thread Giovanni Di Maria via Digitalmars-d-learn
On Saturday, 17 August 2019 at 20:57:44 UTC, Giovanni Di Maria 
wrote:

On Saturday, 17 August 2019 at 20:15:02 UTC, Stefan Koch wrote:
On Saturday, 17 August 2019 at 19:43:22 UTC, Giovanni Di Maria 
wrote:

[...]


Yes they do

A function call has a cost.
In case of a function which performes a 1 cycle  (nominally 
without ILP) operation, the overhead of the function call 
dominates.

try compiling with -inline and compare again.





Hi Stefan Koch
Thank you very much.
With the option -inline, now the execution is very fast,
only 4 seconds
Thnak you thank you very much.
Giovanni



I have also compiled with:
dmd program.d -O -release -inline -boundscheck=off

and the execution speed is very very fast.
Giovanni



Re: Slow UDF call?

2019-08-17 Thread Giovanni Di Maria via Digitalmars-d-learn

On Saturday, 17 August 2019 at 20:15:02 UTC, Stefan Koch wrote:
On Saturday, 17 August 2019 at 19:43:22 UTC, Giovanni Di Maria 
wrote:

[...]


Yes they do

A function call has a cost.
In case of a function which performes a 1 cycle  (nominally 
without ILP) operation, the overhead of the function call 
dominates.

try compiling with -inline and compare again.





Hi Stefan Koch
Thank you very much.
With the option -inline, now the execution is very fast,
only 4 seconds
Thnak you thank you very much.
Giovanni


Slow UDF call?

2019-08-17 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi,
i have seen that a simple operation (in a loop) is faster than 
the equivalent UDF.
The first example takes 4 seconds, the second example takes 16 
seconds.

Local variables influence the speed of execution?
Thank you very much.
Giovanni

==

import std.stdio;
void main()
{
int a,b;
int s;
int k;
writeln("START");
for(k=1;k<=2_000_000_000;k++)
{
a=7;
b=20;
s=a+b;
}
writeln("Fine ",k," ",s);
}

==


import std.stdio;
void main()
{
int a,b;
int s;
int k;
writeln("START");
for(k=1;k<=2_000_000_000;k++)
{
a=7;
b=20;
s=somma(a,b);
}
writeln("Fine ",k," ",s);
}

int somma(int n1,int n2)
{
return n1+n2;
}




Re: Arrays in the disk

2019-08-16 Thread Giovanni Di Maria via Digitalmars-d-learn

On Friday, 16 August 2019 at 07:27:36 UTC, zabruk wrote:

may be std.mmfile can be usefull
https://dlang.org/phobos/std_mmfile.html


Thank you Zabruk.
I will study it.
Giovanni



Arrays in the disk

2019-08-16 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi
Do they exist arrays in the disk?
I must manage very very large data.
Thank you very much.
Giovanni


Re: Speed of Random Numbers

2019-08-03 Thread Giovanni Di Maria via Digitalmars-d-learn

Thank you very much to Everybody!
Giovanni



Re: Speed of Random Numbers

2019-08-03 Thread Giovanni Di Maria via Digitalmars-d-learn

On Saturday, 3 August 2019 at 17:44:44 UTC, lithium iodate wrote:
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:

[...]


First off you could try to use a faster RNG engine than the 
default. The easiest way is to define a variable containing it 
and passing it to the functions each time.


auto rng = Xorshift(1234);
randomNumber = uniform!uint(rng);

This basic change approximately halved the 5 seconds your 
original example needs on my computer.
Another simple approach that I have tried is simply hashing the 
iterator using a fast hash function.
With xxHash32 I got the time down to 0.25 seconds. I also tried 
xxHash64 and FNV1a but they were not faster in my quick test.




Thank you very much Lithium Iodate
Now i will try it.
I let know you.
Thank you
Giovanni


Re: Speed of Random Numbers

2019-08-03 Thread Giovanni Di Maria via Digitalmars-d-learn

On Saturday, 3 August 2019 at 17:17:23 UTC, Cym13 wrote:
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:

[...]


To what extent isn't the quality of randomness important to you?

Your posts reminds me of the way Doom (the original) did it for 
things like enemy behaviour and shot dispersion: they generated 
a static table of 256 random numbers once and any time they 
needed a random byte they just picked the next in the table. 
They didn't have any security or sciency concern and just 
wanted to provide a different game each time so that worked 
well for them. You won't find anything faster than that I think.





Exactly Cym13.
The important is to get a different number
Ok, thank you.
G


Speed of Random Numbers

2019-08-03 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi to everybody
I am doing some experiments about random numbers.
I need "extreme speed" for the generation for numbers from 1 to 8.

Generating 500_000_000 numbers with this code:



-
import std.stdio, std.array, std.random;
void main()
{
byte c;
writeln("Start");
for(int k=1;k<=500_000_000;k++)
c=uniform!ubyte() % 8 +1;  //<<< === RANDOM
writeln("Stop");
}
-



I get these results:

c=uniform!ubyte() % 8 +1;  ==>>> Execution time: 15.563 s
c=cast(byte)uniform(1, 9); ==>>> Execution time: 24.218 s

Do you know other faster functions or methods to generate random 
numbers?


For me the "goodness of random" is NOT important.

Thank you very much
GIovanni Di Maria






Re: Milliseconds

2019-07-12 Thread Giovanni Di Maria via Digitalmars-d-learn

On Friday, 12 July 2019 at 07:21:58 UTC, Jonathan M Davis wrote:
On Friday, July 12, 2019 12:51:28 AM MDT Giovanni Di Maria via 
Digitalmars- d-learn wrote:

[...]


You mean if the time is currently 15:46:52.7205007, you want an 
integer value that's 720?


[...]





Perfect Jonathan .
Thank you very very much!
Giovanni


Milliseconds

2019-07-12 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi
I have read much, before to write here.
How can i store, to an int variable, the milliseconds of the 
current time?

It's simple, but i don't find the solution.
Thank you very much.

Giovanni Di Maria



Re: Read a text file in any position.

2019-01-18 Thread Giovanni Di Maria via Digitalmars-d-learn

On Friday, 18 January 2019 at 08:49:08 UTC, bauss wrote:
On Friday, 18 January 2019 at 08:10:25 UTC, Giovanni Di Maria 
wrote:

Hi All
I have a text file like this:

11
22
33
44
55
66
77
..

I execute these functions:

i = archive.readln();
i = archive.readln();
i = archive.readln();
i = archive.readln();

so "i" is 44.

Is there a method to move the pointer of file to beginning of 
file or to any position (for example back to 2nd row), without 
to close the file?


Thank you very much.
Regards
Giovanni


Use File (https://dlang.org/library/std/stdio/file.html)

And seek() (https://dlang.org/library/std/stdio/file.seek.html)

And then one of its read functions.

rawRead will let you read the raw bytes at the position.





Thank you very very much.
Giovanni


Read a text file in any position.

2019-01-18 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi All
I have a text file like this:

11
22
33
44
55
66
77
..

I execute these functions:

i = archive.readln();
i = archive.readln();
i = archive.readln();
i = archive.readln();

so "i" is 44.

Is there a method to move the pointer of file to beginning of 
file or to any position (for example back to 2nd row), without to 
close the file?


Thank you very much.
Regards
Giovanni







Re: How to reuse the space of RAM of an array of strings?

2018-12-22 Thread Giovanni Di Maria via Digitalmars-d-learn
On Friday, 21 December 2018 at 22:31:26 UTC, Steven Schveighoffer 
wrote:

On 12/21/18 3:41 PM, Giovanni Di Maria wrote:

[...]


Note: alloca is a builtin intrinsic, so I wouldn't use that as 
a function name. Don't think it's affecting your program, but I 
wanted to point that out.



 [...]


This does NOT free the ram, it simply resets vec to null.

If you want to free the memory, use

GC.free(vec.ptr); vec = null;


 [...]


Note, this allocates a lot of smaller arrays on its way up to 
the really big array. You are better off doing:


vec.reserve(5_000_000);

which will pre-allocate the capacity needed. This will make you 
only allocate once.


-Steve








Hi Steve
Ok, thank you very much.
Ciao
Giovanni


Re: How to reuse the space of RAM of an array of strings?

2018-12-21 Thread Giovanni Di Maria via Digitalmars-d-learn

On Friday, 21 December 2018 at 21:28:14 UTC, H. S. Teoh wrote:

[...]







Thank you very much for your replay.
I will meditate about your words.
Thank you
Giovanni


How to reuse the space of RAM of an array of strings?

2018-12-21 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi. Can you help me please?
I asked also to "General Group" but i haven't solved my problem.
I have tried many experiments but without success.
I have a dynamic array of strings:
string[] vec;

After to have inserted dynamically 5_000_000 strings to array, 
for six times, i measure the RAM with an utility (for example 
Wise Memory Optimizer), deleting the array every time.


Every time I destroy the array, but the RAM is always less.
In particular:

- at beginning the FREE RAM is 1564 MB;
- after first loop the FREE RAM IS 1480 MB
- after second loop the FREE RAM IS 1415 MB
- after third loop the FREE RAM IS 1402 MB
- after forth loop the FREE RAM IS 1338 MB
- after fifth loop the FREE RAM IS 1280 MB
- after sixth loop the FREE RAM IS 1200 MB
- at end the FREE RAM returns to 1564 MB

I want to reuse the dynamic array.

This is the program:

import std.stdio;
string[] vec;
void main()
{
alloca();
alloca();
alloca();
alloca();
alloca();
alloca();
}

void alloca()
{
vec.destroy;
writeln("Filling .");
for (int i = 0; i < 500; i++)
vec ~= "1234567890ABCDEFGHIL123456";
writeln("Array occupation: ", vec.capacity);
writeln("Press ");
readln();
}

P.S.
I have tried: free(), destroy(), minimize(), GC, collect(), etc 
but without success.


Can you help me please?
Thank you very much
Giovanni Di Maria


Re: Map of functions

2018-12-16 Thread Giovanni Di Maria via Digitalmars-d-learn

Thank you very much.
Great!!!
Giovanni



Re: Map of functions

2018-12-14 Thread Giovanni Di Maria via Digitalmars-d-learn

Thank you very  much,
Fantastic!!!
Giovanni



Re: Map of functions

2018-12-14 Thread Giovanni Di Maria via Digitalmars-d-learn


Do you really have a nested function print() inside a nested 
function calculate() inside main()?  That is,



Hi
this is only an example of names of funcions but the structure is 
this:


void main()
{
// maybe some calls to calculate()
}

void calculate()
{
// maybe some calls to print()
}

void print()
{
}



I need the flow of calls.
Thank you
Giovanni




Map of functions

2018-12-14 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi
Is there an utility to print
the functions in a source file, for example:

- main()
--- calculate()
- print()
--- simulate()
- print()
.
Thank you very much
Giovanni Di Maria



Re: Print RAM used by arrays

2018-12-14 Thread Giovanni Di Maria via Digitalmars-d-learn

Thank you very much for your
precious informations.
Now i will try.
Thank you!!!
Giovanni



Print RAM used by arrays

2018-12-13 Thread Giovanni Di Maria via Digitalmars-d-learn

Hi.
How can I know the amount of RAM allocated by a vector?

For example:

string[8][1000] array;
for(int i=0;i<1000;i++) {
   
array[i]=["","","","","","","",""];

}

how can I know the amount of bytes of above matrix?
Can I clean the memory ofter his use, without use GC?

Thank you to everybody

Giovanni Di Maria