Python* API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

datastructures_packedtriangular.py

1 # file: datastructures_packedtriangular.py
2 #===============================================================================
3 # Copyright 2014-2020 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #===============================================================================
17 
18 #
19 # ! Content:
20 # ! Python example of using packed data structures
21 # !*****************************************************************************
22 
23 #
24 
25 
26 #
27 from __future__ import print_function
28 
29 import os
30 import sys
31 
32 import numpy as np
33 
34 from daal.data_management import PackedTriangularMatrix, NumericTableIface, BlockDescriptor, readOnly, readWrite
35 
36 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
37 if utils_folder not in sys.path:
38  sys.path.insert(0, utils_folder)
39 from utils import printArray
40 
41 
42 if __name__ == "__main__":
43 
44  print("Packed triangular matrix example")
45  print()
46 
47  nDim = 5
48  firstReadRow = 0
49  nRead = 5
50 
51  # Example of using a packed triangular matrix
52  data = np.array([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4], dtype=np.float64)
53 
54  dataTable = PackedTriangularMatrix(NumericTableIface.lowerPackedTriangularMatrix, data)
55 
56  block = BlockDescriptor()
57 
58  # Read a block of rows
59  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
60  print("{} rows are read".format(block.getNumberOfRows()))
61 
62  printArray(block.getArray(), nDim, block.getNumberOfRows(), block.getNumberOfColumns(),
63  "Print 3 rows from packed triangular matrix as float:")
64 
65  # Read a feature(column) and write into it
66  readFeatureIdx = 2
67  dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nDim, readWrite, block)
68  printArray(block.getArray(), 1, block.getNumberOfRows(), block.getNumberOfColumns(),
69  "Print the third feature of packed triangular matrix:")
70 
71  # Set new value to a buffer and release it
72  dataBlock = block.getArray()
73  dataBlock[readFeatureIdx - 1] = -1
74  dataBlock[readFeatureIdx + 1] = -2
75  dataTable.releaseBlockOfColumnValues(block)
76 
77  # Read a block of rows. Ensure that data has changed
78  dataTable.getBlockOfRows(firstReadRow, nRead, readOnly, block)
79  print("{} rows are read".format(block.getNumberOfRows()))
80  printArray(block.getArray(), nDim, block.getNumberOfRows(), block.getNumberOfColumns(),
81  "Print 3 rows from packed triangular matrix as float:")

For more complete information about compiler optimizations, see our Optimization Notice.