RecArrayCreation ================ .. cpp:class:: numpy::RecArrayCreation numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use RecArrayCreation RecArrayCreation obj; // ... operations ... Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``RecordArray ones(const std::vector&shape, std::shared_ptrdtype)`` - RecordArray - NP_REC_CREATION.H:233 - :ref:`View ` * - ``RecordArray zeros(const std::vector&shape, std::shared_ptrdtype)`` - RecordArray - NP_REC_CREATION.H:208 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``DType getDTypeFromType()`` - DType - NP_REC_CREATION.H:279 - * - ``DType getDTypeFromValue(const StructuredValue &value)`` - DType - NP_REC_CREATION.H:272 - I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``RecordArray fromfile(const std::string &filename, std::shared_ptrdtype, const std::string &separator = ",", size_tcount = SIZE_MAX, boolskip_header = false)`` - RecordArray - NP_REC_CREATION.H:173 - :ref:`View ` * - ``RecordArray fromstring(const std::string &data, std::shared_ptrdtype, const std::string &separator = ",", size_tcount = SIZE_MAX)`` - RecordArray - NP_REC_CREATION.H:155 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``RecordArray array(const std::vector>&data, const std::vector>&field_specs)`` - RecordArray - NP_REC_CREATION.H:202 - :ref:`View ` * - ``RecordArray fromarrays(const std::vector>>&field_arrays)`` - RecordArray - NP_REC_CREATION.H:63 - :ref:`View ` * - ``RecordArray fromarrays(const std::vector>>>&field_data, const std::vector&shape)`` - RecordArray - NP_REC_CREATION.H:107 - :ref:`View ` * - ``RecordArray fromrecords(const std::vector>&records, const std::vector>&field_specs)`` - RecordArray - NP_REC_CREATION.H:14 - :ref:`View ` * - ``RecordArray fromrecords(const std::vector&records, std::shared_ptrdtype)`` - RecordArray - NP_REC_CREATION.H:31 - :ref:`View ` * - ``RecordArray fromrecords(const std::vector&records)`` - RecordArray - NP_REC_CREATION.H:46 - :ref:`View ` * - ``StructuredRecord parseRecordFromString(const std::string &line, std::shared_ptrdtype, const std::string &separator)`` - StructuredRecord - NP_REC_CREATION.H:320 - * - ``StructuredValue parseValueFromString(const std::string &str, DTypedtype)`` - StructuredValue - NP_REC_CREATION.H:342 - * - ``void setFieldFromRawData(RecordArray &array, const std::vector&indices, const std::string &field_name, DTypedtype, void \*data, size_toffset)`` - void - NP_REC_CREATION.H:296 - * - ``void setFieldFromTupleElement(RecordArray &array, const std::vector&indices, const std::string &field_name, const T &value)`` - void - NP_REC_CREATION.H:266 - * - ``void setOneValue(RecordArray &array, const std::vector&indices, const std::string &field_name, DTypedtype)`` - void - NP_REC_CREATION.H:393 - * - ``void setRecordFromTuple(RecordArray &array, const std::vector&indices, const Tuple &tuple, std::index_sequence)`` - void - NP_REC_CREATION.H:259 - * - ``void setZeroValue(RecordArray &array, const std::vector&indices, const std::string &field_name, DTypedtype)`` - void - NP_REC_CREATION.H:373 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-recarraycreation-ones-0: .. dropdown:: ones (np_test_1_all.cpp:319) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 309 :emphasize-lines: 11 //int_range.printArray(); auto reshaped = int_range.reshapeArray({ 3, 2 }); // std::cout << "Reshaped to 2x2 matrix:"; //reshaped.printArray(); auto zeros = createInt32Zeros({ 2, 3 }); // std::cout << "zeros (2,3):"; //zeros.printArray(); auto ones = createInt32Ones({ 2, 3 }); // std::cout << "ones (2,3):"; //ones.printArray(); // std::cout << "Addition of zeros and ones:" << std::endl; auto sum = zeros.addArrays(ones); //sum.printArray(); // std::cout << "Sum of all elements: " << sum.sumArray() << std::endl; // std::cout << "Mean: " << sum.meanArray() << std::endl; // std::cout << std::endl << "Type introspection with get_typename():" << std::endl; .. _example-recarraycreation-zeros-1: .. dropdown:: zeros (np_test_1_all.cpp:316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 306 :emphasize-lines: 11 auto int_range = NDArray::createRange(1, 12, 2); // std::cout << "Int32 range array (1 to 10, step 2):" << std::endl; //int_range.printArray(); auto reshaped = int_range.reshapeArray({ 3, 2 }); // std::cout << "Reshaped to 2x2 matrix:"; //reshaped.printArray(); auto zeros = createInt32Zeros({ 2, 3 }); // std::cout << "zeros (2,3):"; //zeros.printArray(); auto ones = createInt32Ones({ 2, 3 }); // std::cout << "ones (2,3):"; //ones.printArray(); // std::cout << "Addition of zeros and ones:" << std::endl; auto sum = zeros.addArrays(ones); //sum.printArray(); // std::cout << "Sum of all elements: " << sum.sumArray() << std::endl; .. _example-recarraycreation-fromfile-2: .. dropdown:: fromfile (np_test_1_all.cpp:26167) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26157 :emphasize-lines: 11 outfile.close(); // Define dtype std::vector> fields = { {"id", numpy::DType::INT32}, {"value", numpy::DType::FLOAT64} }; auto dtype = std::make_shared(fields); // Load from file auto result = numpy::fromfile(filename, dtype, ","); // Verify loaded correctly bool passed = (result.getSize() == 3); if (passed) { passed = (result.getFieldValue({ 0 }, "id") == 1); passed = passed && (std::abs(result.getFieldValue({ 1 }, "value") - 20.5) < 0.001); } // Clean up temp file std::remove(filename.c_str()); .. _example-recarraycreation-fromstring-3: .. dropdown:: fromstring (np_test_1_all.cpp:21885) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21875 :emphasize-lines: 11 std::cout << "========= testStringParsing ======================="; auto dtype = std::make_shared(std::vector>{ {"name", DType::UNICODE32}, { "age", DType::INT32 }, { "score", DType::FLOAT64 } }); std::string data = "Alice,25,85.5\nBob,30,92.3\nCharlie,35,78.9"; auto arr = fromstring(data, dtype, ",", 3); // std::cout << "Array created from string data:" << std::endl; //arr.printArray(); auto name_1 = arr.getFieldValue({ 1 }, "name"); auto age_1 = arr.getFieldValue({ 1 }, "age"); auto score_1 = arr.getFieldValue({ 1 }, "score"); // std::cout << "Verification of record 1:" << std::endl; // std::cout << "Name: " << (name_1.size() > 0 ? "Bob" : "empty") << std::endl; .. _example-recarraycreation-array-4: .. dropdown:: array (np_test_1_all.cpp:243) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 233 :emphasize-lines: 11 // std::cout << "Min: " << array.minArray() << std::endl; // std::cout << "Max: " << array.maxArray() << std::endl; std::cout << " -> tests passed" << std::endl; } void testReshapeAndFlatten() { std::cout << "========= testReshapeAndFlatten ======================="; auto range_array = NDArray::createRange(0, 12, 1); // std::cout << "Range array (0-11):" << std::endl; //range_array.printArray(); auto reshaped = range_array.reshapeArray({3, 4}); // std::cout << "Reshaped to 3x4:"; //reshaped.printArray(); auto flattened = reshaped.flattenArray(); // std::cout << "Flattened:" << std::endl; //flattened.printArray(); .. _example-recarraycreation-fromarrays-5: .. dropdown:: fromarrays (np_test_1_all.cpp:21655) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21645 :emphasize-lines: 11 ids.setElementAt({ 2 }, 300); NDArray scores({ 3 }); scores.setElementAt({ 0 }, 85.5); scores.setElementAt({ 1 }, 92.3); scores.setElementAt({ 2 }, 78.9); std::vector>> int_arrays = { {"id", ids} }; std::vector>> float_arrays = { {"score", scores} }; auto arr1 = fromarrays(int_arrays); auto arr2 = fromarrays(float_arrays); // std::cout << "Array from int arrays:" << std::endl; //arr1.printArray(); // std::cout << "Array from float arrays:" << std::endl; //arr2.printArray(); auto id_check = arr1.getFieldValue({ 1 }, "id"); auto score_check = arr2.getFieldValue({ 2 }, "score"); .. _example-recarraycreation-fromarrays-6: .. dropdown:: fromarrays (np_test_1_all.cpp:21655) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21645 :emphasize-lines: 11 ids.setElementAt({ 2 }, 300); NDArray scores({ 3 }); scores.setElementAt({ 0 }, 85.5); scores.setElementAt({ 1 }, 92.3); scores.setElementAt({ 2 }, 78.9); std::vector>> int_arrays = { {"id", ids} }; std::vector>> float_arrays = { {"score", scores} }; auto arr1 = fromarrays(int_arrays); auto arr2 = fromarrays(float_arrays); // std::cout << "Array from int arrays:" << std::endl; //arr1.printArray(); // std::cout << "Array from float arrays:" << std::endl; //arr2.printArray(); auto id_check = arr1.getFieldValue({ 1 }, "id"); auto score_check = arr2.getFieldValue({ 2 }, "score"); .. _example-recarraycreation-fromrecords-7: .. dropdown:: fromrecords (np_test_1_all.cpp:21622) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21612 :emphasize-lines: 11 record2.set("value", static_cast(20.7)); record2.set("active", static_cast(false)); records.push_back(record2); StructuredRecord record3; record3.set("id", static_cast(3)); record3.set("value", static_cast(15.3)); record3.set("active", static_cast(true)); records.push_back(record3); auto arr = fromrecords(records); // std::cout << "Array created from records:" << std::endl; //arr.printArray(); auto id_1 = arr.getFieldValue({ 1 }, "id"); auto value_2 = arr.getFieldValue({ 2 }, "value"); auto active_0 = arr.getFieldValue({ 0 }, "active"); // std::cout << "Verification:" << std::endl; // std::cout << "Record 1 ID: " << id_1 << " (expected: 2)" << std::endl; .. _example-recarraycreation-fromrecords-8: .. dropdown:: fromrecords (np_test_1_all.cpp:21622) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21612 :emphasize-lines: 11 record2.set("value", static_cast(20.7)); record2.set("active", static_cast(false)); records.push_back(record2); StructuredRecord record3; record3.set("id", static_cast(3)); record3.set("value", static_cast(15.3)); record3.set("active", static_cast(true)); records.push_back(record3); auto arr = fromrecords(records); // std::cout << "Array created from records:" << std::endl; //arr.printArray(); auto id_1 = arr.getFieldValue({ 1 }, "id"); auto value_2 = arr.getFieldValue({ 2 }, "value"); auto active_0 = arr.getFieldValue({ 0 }, "active"); // std::cout << "Verification:" << std::endl; // std::cout << "Record 1 ID: " << id_1 << " (expected: 2)" << std::endl; .. _example-recarraycreation-fromrecords-9: .. dropdown:: fromrecords (np_test_1_all.cpp:21622) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 21612 :emphasize-lines: 11 record2.set("value", static_cast(20.7)); record2.set("active", static_cast(false)); records.push_back(record2); StructuredRecord record3; record3.set("id", static_cast(3)); record3.set("value", static_cast(15.3)); record3.set("active", static_cast(true)); records.push_back(record3); auto arr = fromrecords(records); // std::cout << "Array created from records:" << std::endl; //arr.printArray(); auto id_1 = arr.getFieldValue({ 1 }, "id"); auto value_2 = arr.getFieldValue({ 2 }, "value"); auto active_0 = arr.getFieldValue({ 0 }, "active"); // std::cout << "Verification:" << std::endl; // std::cout << "Record 1 ID: " << id_1 << " (expected: 2)" << std::endl;