vm_obj put_nat(vm_obj const & n, vm_obj const &) { if (is_simple(n)) std::cout << cidx(n); else std::cout << to_mpz(n); return mk_vm_unit(); }
vm_obj nat_succ(vm_obj const & a) { if (is_simple(a)) { return mk_vm_nat(cidx(a) + 1); } else { return mk_vm_mpz(to_mpz1(a) + 1); } }
vm_obj nat_mod(vm_obj const & a1, vm_obj const & a2) { if (is_simple(a1) && is_simple(a2)) { unsigned v1 = cidx(a1); unsigned v2 = cidx(a2); if (v2 == 0) return a1; else return mk_vm_nat(v1 % v2); } else { mpz const & v1 = to_mpz1(a1); mpz const & v2 = to_mpz2(a2); if (v2 == 0) return a1; else return mk_vm_nat(v1 % v2); } }
vm_obj nat_sub(vm_obj const & a1, vm_obj const & a2) { if (is_simple(a1) && is_simple(a2)) { unsigned v1 = cidx(a1); unsigned v2 = cidx(a2); if (v2 > v1) return mk_vm_simple(0); else return mk_vm_nat(v1 - v2); } else { mpz const & v1 = to_mpz1(a1); mpz const & v2 = to_mpz2(a2); if (v2 > v1) return mk_vm_simple(0); else return mk_vm_nat(v1 - v2); } }
vm_obj nat_mul(vm_obj const & a1, vm_obj const & a2) { if (is_simple(a1) && is_simple(a2)) { unsigned long long r = static_cast<unsigned long long>(cidx(a1)) * static_cast<unsigned long long>(cidx(a2)); if (r < LEAN_MAX_SMALL_NAT) { return mk_vm_simple(r); } } return mk_vm_mpz(to_mpz1(a1) * to_mpz2(a2)); }
static mpz const & to_mpz2(vm_obj const & o) { if (is_simple(o)) { mpz & r = get_mpz2(); r = cidx(o); return r; } else { return to_mpz(o); } }
vm_obj nat_to_string(vm_obj const & a) { std::ostringstream out; if (is_simple(a)) { out << cidx(a); } else { out << to_mpz(a); } return to_obj(out.str()); }
optional<unsigned> try_to_unsigned(vm_obj const & o) { if (is_simple(o)) { return optional<unsigned>(cidx(o)); } else { mpz const & v = to_mpz(o); if (v.is_unsigned_int()) return optional<unsigned>(v.get_unsigned_int()); else return optional<unsigned>(); } }
// ------------------------------------------------------------- // MatConvertGAtoDense // ------------------------------------------------------------- PetscErrorCode MatConvertGAToDense(Mat A, Mat *B) { PetscErrorCode ierr = 0; MPI_Comm comm; int nproc; struct MatGACtx *ctx; PetscInt lrows, grows, lcols, gcols, lo, hi; ierr = PetscObjectGetComm((PetscObject)A, &comm); CHKERRQ(ierr); ierr = MPI_Comm_size(comm, &nproc); ierr = MatShellGetContext(A, &ctx); CHKERRQ(ierr); ierr = MatGetSize(A, &grows, &gcols); CHKERRQ(ierr); ierr = MatGetLocalSize(A, &lrows, &lcols); CHKERRQ(ierr); ierr = MatCreateDense(comm, lrows, lcols, grows, gcols, NULL, B); CHKERRQ(ierr); ierr = MatCreate(comm, B); CHKERRXX(ierr); ierr = MatSetSizes(*B, lrows, lcols, grows, gcols); CHKERRXX(ierr); if (nproc == 1) { ierr = MatSetType(*B, MATSEQDENSE); CHKERRXX(ierr); ierr = MatSeqDenseSetPreallocation(*B, PETSC_NULL); CHKERRXX(ierr); } else { ierr = MatSetType(*B, MATDENSE); CHKERRXX(ierr); ierr = MatMPIDenseSetPreallocation(*B, PETSC_NULL); CHKERRXX(ierr); } ierr = MatGetOwnershipRange(*B, &lo, &hi); CHKERRQ(ierr); std::vector<PetscInt> cidx(gcols); for (PetscInt c = 0; c < gcols; ++c) { cidx[c] = c; } std::vector<PetscScalar> rowvals(gcols); for (PetscInt r = lo; r < hi; ++r) { int glo[2] = {r, 0}; int ghi[2] = {r, gcols - 1}; int ld[2] = {1,1}; NGA_Get(ctx->ga, glo, ghi, &rowvals[0], ld); ierr = MatSetValues(*B, 1, &r, gcols, &cidx[0], &rowvals[0], INSERT_VALUES); CHKERRQ(ierr); } ierr = MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); ierr = MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); return ierr; }
vm_obj nat_repeat(vm_obj const &, vm_obj const & f, vm_obj const & n, vm_obj const & a) { if (is_simple(n)) { unsigned _n = cidx(n); vm_obj r = a; for (unsigned i = 0; i < _n ; i++) { r = invoke(f, mk_vm_simple(i), r); } return r; } else { mpz _n = to_mpz(n); mpz i(0); vm_obj r = a; while (i < _n) { r = invoke(f, mk_vm_nat(i), r); i++; } return r; } }
vm_obj format_of_nat(vm_obj const & n) { if (is_simple(n)) return to_obj(format(cidx(n))); else return to_obj(format(to_mpz(n).to_string())); }
vm_obj format_highlight(vm_obj const & fmt, vm_obj const & c) { return to_obj(highlight(to_format(fmt), static_cast<format::format_color>(cidx(c)))); }
unsigned to_unsigned(vm_obj const & o) { if (is_simple(o)) return cidx(o); else return to_mpz(o).get_unsigned_int(); }