Skip to content

Commit e55b698

Browse files
author
dotchris90
committed
Doc : go on with docs - explained NDArray ideas
1 parent 224932d commit e55b698

File tree

4 files changed

+95
-115
lines changed

4 files changed

+95
-115
lines changed

docfx_project/articles/NDArray.Creation.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,58 @@ Before we do some fancy numeric stuff or even machine learning we have to clear
77
Since NDArray is the key class in SciSharp stack there must be numerous possibilities how to generate this arrays. And yes that’s the case.
88

99
Maybe first of all we should see the dump way – which can be always used but is not too user friendly.
10+
In this example we access the Storage property directly - one more reason to avoid it.
1011

1112
**Dump way**
1213

1314
```CSHARP
14-
// first constructor with data type
15-
// you also specify the generic type of the NDArray
16-
var np = new NDArray(typeof(double));
17-
18-
// set 9 elements into this array
15+
// first constructor with data type and shape (here 3x3 matrix)
16+
var nd = new NDArray(typeof(double),3,3);
17+
18+
// set 9 elements into the storage of this array.
1919
np.Storage.SetData(new double[] {1,2,3,4,5,6,7,8,9});
20-
21-
// shape it to a 3x3 matrix
22-
np.Storage.Shape = new Shape(3,3);
2320
```
2421

2522
Ok looks not too difficult. But also not too userfriendly.
2623

27-
We create an empty NDArray< double >, fill it with 9 elements and shape it to 3x3 matrix.
28-
29-
At this point we could shape it also to a 1x9 vector but we shaped it to a 3x3.
24+
We create an empty NDArray with 3x3 shape, fill it with 9 elements.
25+
We followed the row wise matrix layour by default.
3026

31-
So with this 3x3 shaped NDArray we can do matrix multiplication but with a 1x9 we could not.
27+
So with this 3x3 shaped NDArray we can do matrix multiplication, QR decomposition, SVD, ...
3228

33-
Always be careful with your shape and be sure what you want to do with your elements.
29+
But keep in mind - always be careful with your shape and be sure what you want to do with your elements in this shape.
3430

3531
**Create by enumeration**
3632

33+
The next example shows the numpy style creation.
34+
3735
```CSHARP
38-
// at this point we use a numpy class so it looks like numpy function call
39-
var np = new Numpy<double>();
36+
using NumSharp.Core;
4037

4138
// we take the Data / elements from an array
42-
// we do not need to define the shape here
39+
// we do not need to define the shape here - it is automaticly shaped to 1D
4340
var np1 = np.array(new double[] {1,2,3,4,5,6} );
4441
```
4542

4643
Ok as we can see, this time the array was created without define the shape.
4744

4845
This is possible since the method expect that the double[] array shall be transformed into a NDArray directly.
4946

47+
**Create by implicit cast**
48+
49+
Beside this numpy style C# offers its own flavour of creation.
50+
51+
```CSHARP
52+
using NumSharp.Core;
53+
54+
// implicit cast double[] to NDArray - dtype & shape are deduced by array type and shape
55+
NDArray nd = new double[]{1,2,3,4};
56+
```
57+
5058
**Create by given range**
5159

5260
```CSHARP
53-
var np = new Numpy<double>();
61+
using NumSharp.Core;
5462

5563
// we simple say "create an array with 10 elements"
5664
// start is expected to be 0 and step 1

docfx_project/articles/intro.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
11
# Introduction
22

33
The following pages are for the users who want to use NumSharp.
4+
5+
Before you read the code examples you should read this page which explain some basis concepts.
6+
An other reference can be numpy since we try our best to follow their APIs.
7+
8+
## NDArray, NDStorgage and Shape
9+
10+
The 3 main classes in NumSharp are NDArray, NDStorage and Shape.
11+
If you want to have a better understanding for NumSharp, you can read the following lines to see how all works together.
12+
13+
Let's start with the question - what is a Tensor?
14+
15+
From programming point of view a tensor is a multi-dimensional array (scalar, vector, matrix, ...) mostly for numerical data like int32, int64, doubles, ... which can be accessed via indexes like np[idx], np[idx,jdx], np[idx,jdx,kdx], ... depending on its dimension.
16+
17+
Ok - in this sentence we got already some properties.
18+
19+
- a tensor is an object for storing (mostly) numerical data
20+
- a tensor has a dimension
21+
- the dimension decides how many indexes are necessary to access the stored data
22+
23+
Each tensor type (dimension 1 - vector, dimension 2 - matrix, ...) has its own .NET type like double[,].
24+
25+
NumSharp brings its own tensor / array type called **NDArray**.
26+
27+
So now the question - .NET offers already multi-dimensional arrays - why a new array type?
28+
29+
NumSharps NDArray offers the capability of storing any tensor (independent of dimension!) into its internal storage.
30+
So NumSharps NDArray can store a vector, a matrix or sth with dimension 5 and higher. This is not possible with .NET arrays since each tensor type is a different class.
31+
32+
Now the next question - how a NDArray can do this?
33+
34+
First of all we need to be a little bit more abstract. Why we use tensors? Because we want to store data and we want to get them. How we get and set them? We get and set via indexes (which are always integers). So just this data are important and the corresponding indexes.
35+
36+
With this in mind we easily can understand the NDStorage of NumSharp.
37+
38+
NDStorage is an object which stores the data of a tesor in a single 1D array. Since it is a 1D array independend of the tensor dimension NDStorage can be used for all kind of tensors.
39+
40+
**But hold on! How the data comes into this 1D array?**
41+
42+
NDStorage has a property called "shape". The shape is a small but important class in NumSharp. It stores the dimensions and most important! it determines which element in the 1D array is selected by given indexes.
43+
44+
45+
46+
47+
48+
49+
50+

src/NumSharp.Core/Generics/Casting/NdArray.Implicit.cs

Lines changed: 0 additions & 98 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using NumSharp.Core.Extensions;
6+
using NumSharp.Core;
7+
8+
namespace NumSharp.UnitTest
9+
{
10+
[TestClass]
11+
public class DocTest
12+
{
13+
[TestMethod]
14+
public void Dump()
15+
{
16+
var nd = new NDArray(typeof(double),3,3);
17+
nd.Storage.SetData(new double[] {1,2,3,4,5,6,7,8,9} );
18+
19+
20+
}
21+
22+
}
23+
}

0 commit comments

Comments
 (0)