TensorBolt
ndarray.h
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2019 by Soulaymen Chouri *
3  * *
4  * This file is part of TensorBolt. *
5  * *
6  * What follows is the Modified BSD License. *
7  * See also http://www.opensource.org/licenses/BSD-3-Clause *
8  * Copyright (c) 2019, Soulaymen Chouri. All rights reserved. *
9  * Redistribution and use in source and binary forms, with or without *
10  * modification, are permitted provided that the following conditions *
11  * are met: *
12  * *
13  * 1. Redistributions of source code must retain the above copyright *
14  * notice, this list of conditions and the following disclaimer. *
15  * *
16  * 2. Redistributions in binary form must reproduce the above *
17  * copyright notice, this list of conditions and the following *
18  * disclaimer in the documentation and/or other materials provided *
19  * with the distribution. *
20  * *
21  * 3. Neither the name of the author nor the names of other *
22  * contributors may be used to endorse or promote products derived *
23  * from this software without specific prior written permission. *
24  * *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR *
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
28  * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, *
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING *
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
35  * POSSIBILITY OF SUCH DAMAGE. *
36  ****************************************************************************/
37 
46 #ifndef _TB_NDARRAY_H_
47 #define _TB_NDARRAY_H_
48 
49 #include <stdint.h>
50 #include <stdarg.h>
51 
52 #ifndef __FUNCTION_NAME__
53 #ifdef WIN32 //WINDOWS
54 #define __FUNCTION_NAME__ __FUNCTION__
55 #else //*NIX
56 #define __FUNCTION_NAME__ __func__
57 #endif
58 #endif
59 
60 #define TB_ASSERT_LOG
61 
62 #if defined(TB_ASSERT_STANDARD)
63 #define ASSERT(c ,msg, ...) assert(c)
64 #elif defined (TB_ASSERT_LOG)
65 #define ASSERT(c, msg, ...) nda_assert(c, #c, __FUNCTION_NAME__ , msg, ##__VA_ARGS__)
66 #elif defined (TB_ASSERT_NONE)
67 #define ASSERT(c, msg, ...)
68 #else
69 #error Please define an assertion policy.
70 #endif
71 
72 
73 /*
74  * change this to double if you need
75  */
76 #define TB_FLOAT 0
77 #define TB_DOUBLE 1
78 
79 #define TB_TYPE TB_FLOAT
80 
81 #if TB_TYPE == TB_FLOAT
82 typedef float tb_float;
83 #else
84 typedef double tb_float;
85 #endif
86 
90 struct NDShape;
91 
95 struct NDShapeStack;
96 
103 void nda_ShapeStackInit(struct NDShapeStack* stack, struct NDShape* shape);
104 
110 uint8_t nda_ShapeStackCanPop(struct NDShapeStack* stack);
111 
117 uint64_t nda_ShapeStackPop(struct NDShapeStack* stack);
118 
122 typedef enum NDArrayLocation {
126 
130 struct NDArray;
131 
138 struct NDShape* nda_newShape(uint64_t rank, ...);
139 
147 struct NDShape* nda_newShapeFromArray(uint64_t rank, uint64_t* dims);
148 
156 struct NDShape* nda_newShapeFromArrayCopy(uint64_t rank, uint64_t* dims);
157 
162 void nda_debugShape(struct NDShape* shape);
163 
169 struct NDShape* nda_copyShape(struct NDShape* shape);
170 
184 uint8_t nda_shapeCanBroadCast(struct NDShape* shape1, struct NDShape* shape2);
185 
191 char* nda_shapeToString(struct NDShape* shape);
192 
197 void nda_debugValue(struct NDArray* tensor);
198 
204 uint64_t nda_getTotalSize(struct NDShape* shape);
205 
211 struct NDArray* nda_alloc(struct NDShape* shape);
212 
221 struct NDArray* nda_randomNormal(struct NDShape* shape, float mu, float sig);
222 
230 struct NDArray* nda_linspace(tb_float a, tb_float b, uint64_t n);
231 
237 struct NDArray* nda_ones(struct NDShape* shape);
238 
245 struct NDArray* nda_fill(struct NDShape* shape, tb_float value);
246 
252 struct NDArray* nda_copy(struct NDArray* x);
253 
259 void nda_reshape(struct NDArray* x, struct NDShape* shape);
260 
265 void nda_free(struct NDArray* array);
266 
273 tb_float nda_get(struct NDArray* array, uint64_t* index);
274 
275 
283 struct NDArray* nda_slice(struct NDArray* array, uint64_t* index);
284 
285 
296 tb_float nda_vget(struct NDArray* array, uint64_t* index, struct NDShape* vshape);
297 
304 tb_float nda_get1D(struct NDArray* array, uint64_t index);
305 
313 tb_float nda_vget1D(struct NDArray* array, uint64_t index);
314 
315 
316 #endif
uint8_t nda_shapeCanBroadCast(struct NDShape *shape1, struct NDShape *shape2)
Verifies if two shapes can be broadcasted Broadcast verifications follows numpy rules:
Tensor Shape DO NOT MANUALLY MODIFY ANY DATA ON THIS STRUCTURE. I will find you.
Definition: ndarray_std.h:55
uint64_t rank
Definition: ndarray_std.h:56
struct NDArray * nda_ones(struct NDShape *shape)
Creates an One initialized tensor.
Tensor data structure.
Definition: ndarray_std.h:73
Definition: ndarray.h:124
tb_float nda_get(struct NDArray *array, uint64_t *index)
Returns the value of an array.
struct NDShape * nda_newShapeFromArray(uint64_t rank, uint64_t *dims)
struct NDArray * nda_randomNormal(struct NDShape *shape, float mu, float sig)
Creates an array from a Guassian Distribution.
NDShape * shape
Definition: ndarray_std.h:75
Treats NDShape as a stack to pop elements, does not modify the original shape.
Definition: ndarray_std.h:65
void nda_ShapeStackInit(struct NDShapeStack *stack, struct NDShape *shape)
Unline other factory pattern object, the stack is usually allocated on the stack (in contrast to heap...
struct NDArray * nda_fill(struct NDShape *shape, tb_float value)
Creates an array initialized with one value.
char * nda_shapeToString(struct NDShape *shape)
Generate a string representation of the shape, which can be used for debugging or generating errors.
struct NDShape * nda_copyShape(struct NDShape *shape)
Creates and Allocates a shape with the same properties of the given one.
uint64_t nda_ShapeStackPop(struct NDShapeStack *stack)
Pops the next element in the stack, does not modify the shape but updates the index in the stack.
tb_float nda_vget1D(struct NDArray *array, uint64_t index)
Returns the value of array throughout a 1D index. If the index has a value higher than the original s...
void nda_reshape(struct NDArray *x, struct NDShape *shape)
reshape an ndarray, old shape is freed.
struct NDArray * nda_slice(struct NDArray *array, uint64_t *index)
Returns a slice of an array.
struct NDArray * nda_copy(struct NDArray *x)
copies an existent tensor, memory must be explicitly freed.
uint64_t nda_getTotalSize(struct NDShape *shape)
Calculate the total number of elements in a tensor shape.
void nda_free(struct NDArray *array)
frees an NDArray alongside its shape
NDArrayLocation
Definition: ndarray.h:122
NDShape * shape
Definition: ndarray_std.h:66
struct NDShape * nda_newShape(uint64_t rank,...)
Definition: ndarray.h:123
uint64_t * dims
Definition: ndarray_std.h:57
void nda_debugValue(struct NDArray *tensor)
Prints tensor value to stdout.
struct NDArray * nda_alloc(struct NDShape *shape)
Creates an empty zeroed tensor.
tb_float nda_vget(struct NDArray *array, uint64_t *index, struct NDShape *vshape)
Returns the value of array throughout a virtual shape. The virtual shape has the same size of the ori...
struct NDArray * nda_linspace(tb_float a, tb_float b, uint64_t n)
Return evenly spaced numbers over a specified interval.
uint8_t nda_ShapeStackCanPop(struct NDShapeStack *stack)
Checks if a shape stack can still popped further more.
void nda_debugShape(struct NDShape *shape)
Prints tensor shape to stdout.
struct NDShape * nda_newShapeFromArrayCopy(uint64_t rank, uint64_t *dims)
tb_float nda_get1D(struct NDArray *array, uint64_t index)
Returns the value of an array through 1d Inde.