Macaulay2 Engine
Loading...
Searching...
No Matches

◆ random() [2/2]

Matrix * Matrix::random ( const Ring * R,
int r,
int c,
double fraction_non_zero,
int special_type )
static

Definition at line 714 of file matrix.cpp.

720{
721 bool doing_fraction = false;
722 int threshold = 0;
723
724 FreeModule *F = R->make_FreeModule(r);
726 MatrixConstructor mat(F, G, nullptr);
727
728 // Loop through all selected elements, flip a 'fraction_non_zero' coin, and if
729 // non-zero
730 // set that element.
731
732 if (density != 1.0)
733 {
734 doing_fraction = true;
735 threshold = static_cast<int>(density * 10000);
736 }
737
738 if (special_type == 0)
739 {
740 for (int i = 0; i < c; i++)
741 for (int j = 0; j < r; j++)
742 {
743 if (doing_fraction)
744 {
745 int32_t u = rawRandomInt((int32_t)10000);
746 if (u > threshold) continue;
747 }
748 try {
749 mat.set_entry(j, i, R->random());
750 } catch (const exc::engine_error& e) {
751 ERROR(e.what());
752 return nullptr;
753 }
754 }
755 }
756 else if (special_type == 1)
757 {
758 for (int i = 0; i < c; i++)
759 {
760 int top = (i >= r ? r : i);
761 for (int j = 0; j < top; j++)
762 {
763 if (doing_fraction)
764 {
765 int32_t u = rawRandomInt((int32_t)10000);
766 if (u > threshold) continue;
767 }
768 try {
769 mat.set_entry(j, i, R->random());
770 } catch (const exc::engine_error& e) {
771 ERROR(e.what());
772 return nullptr;
773 }
774 }
775 }
776 }
777 return mat.to_matrix();
778}
friend class FreeModule
Definition matrix.hpp:73
friend class MatrixConstructor
Definition matrix.hpp:76
virtual FreeModule * make_FreeModule() const
Definition ring.cpp:53
virtual ring_elem random() const
Definition ring.cpp:284
const int ERROR
Definition m2-mem.cpp:55
int32_t rawRandomInt(int32_t max)
Definition random.cpp:44
tbb::flow::graph G

References ERROR, FreeModule, G, Ring::make_FreeModule(), Matrix(), MatrixConstructor, random(), Ring::random(), rawRandomInt(), MatrixConstructor::set_entry(), and MatrixConstructor::to_matrix().