本文整理汇总了C++中af::randu方法的典型用法代码示例。如果您正苦于以下问题:C++ af::randu方法的具体用法?C++ af::randu怎么用?C++ af::randu使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类af
的用法示例。
在下文中一共展示了af::randu方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: gforSet
TEST(JIT, CPP_Multi_strided)
{
const int num = 1024;
gforSet(true);
array a = randu(num, 1, s32);
array b = randu(1, num, s32);
array x = a + b;
array y = a - b;
eval(x, y);
gforSet(false);
vector<int> ha(num);
vector<int> hb(num);
vector<int> hx(num * num);
vector<int> hy(num * num);
a.host(&ha[0]);
b.host(&hb[0]);
x.host(&hx[0]);
y.host(&hy[0]);
for (int j = 0; j < num; j++) {
for (int i = 0; i < num; i++) {
ASSERT_EQ((ha[i] + hb[j]), hx[j*num + i]);
ASSERT_EQ((ha[i] - hb[j]), hy[j*num + i]);
}
}
}
示例2: randu
TEST(JIT, CPP_Multi_pre_eval)
{
const int num = 1 << 16;
array a = randu(num, s32);
array b = randu(num, s32);
array x = a + b;
array y = a - b;
eval(x);
// Should evaluate only y
eval(x, y);
// Should not evaluate anything
// Should not error out
eval(x, y);
vector<int> ha(num);
vector<int> hb(num);
vector<int> hx(num);
vector<int> hy(num);
a.host(&ha[0]);
b.host(&hb[0]);
x.host(&hx[0]);
y.host(&hy[0]);
for (int i = 0; i < num; i++) {
ASSERT_EQ((ha[i] + hb[i]), hx[i]);
ASSERT_EQ((ha[i] - hb[i]), hy[i]);
}
}
示例3: randu
TEST(MatrixMultiply, RhsBroadcastBatched)
{
const int M = 512;
const int K = 512;
const int N = 10;
const int D2 = 2;
const int D3 = 3;
for (int d3 = 1; d3 <= D3; d3 *= D3) {
for (int d2 = 1; d2 <= D2; d2 *= D2) {
array a = randu(M, K, d2, d3);
array b = randu(K, N);
array c = matmul(a, b);
for (int j = 0; j < d3; j++) {
for (int i = 0; i < d2; i++) {
array a_ij = a(span, span, i, j);
array c_ij = c(span, span, i, j);
array res = matmul(a_ij, b);
EXPECT_LT(max<float>(abs(c_ij - res)), 1E-3)
<< " for d2 = " << d2 << " for d3 = " << d3;
}
}
}
}
}
示例4: selectTest
void selectTest(const dim4 &dims)
{
if (noDoubleTests<T>()) return;
dtype ty = (dtype)dtype_traits<T>::af_type;
array a = randu(dims, ty);
array b = randu(dims, ty);
if (a.isinteger()) {
a = (a % (1 << 30)).as(ty);
b = (b % (1 << 30)).as(ty);
}
array cond = randu(dims, ty) > a;
array c = select(cond, a, b);
int num = (int)a.elements();
vector<T> ha(num);
vector<T> hb(num);
vector<T> hc(num);
vector<char> hcond(num);
a.host(&ha[0]);
b.host(&hb[0]);
c.host(&hc[0]);
cond.host(&hcond[0]);
for (int i = 0; i < num; i++) {
ASSERT_EQ(hc[i], hcond[i] ? ha[i] : hb[i]);
}
}
示例5: selectScalarTest
void selectScalarTest(const dim4 &dims)
{
if (noDoubleTests<T>()) return;
dtype ty = (dtype)dtype_traits<T>::af_type;
array a = randu(dims, ty);
array cond = randu(dims, ty) > a;
double b = 3;
if (a.isinteger()) {
a = (a % (1 << 30)).as(ty);
}
array c = is_right ? select(cond, a, b) : select(cond, b, a);
int num = (int)a.elements();
vector<T> ha(num);
vector<T> hc(num);
vector<char> hcond(num);
a.host(&ha[0]);
c.host(&hc[0]);
cond.host(&hcond[0]);
if (is_right) {
for (int i = 0; i < num; i++) {
ASSERT_EQ(hc[i], hcond[i] ? ha[i] : T(b));
}
} else {
for (int i = 0; i < num; i++) {
ASSERT_EQ(hc[i], hcond[i] ? T(b) : ha[i]);
}
}
}
示例6: randu
TEST(JoinMany0, CPP) {
array a0 = randu(10, 5);
array a1 = randu(20, 5);
array a2 = randu(5, 5);
array output = join(0, a0, a1, a2);
array gold = join(0, a0, join(0, a1, a2));
ASSERT_EQ(sum<float>(output - gold), 0);
}
示例7: randu
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_transpose) {
//! [ex_matrix_manipulation_transpose]
array x = randu(2, 2, f32);
af_print(x.T()); // transpose (real)
array c = randu(2, 2, c32);
af_print(c.T()); // transpose (complex)
af_print(c.H()); // Hermitian (conjugate) transpose
//! [ex_matrix_manipulation_transpose]
}
示例8: deviceGC
TEST(Join, JoinLargeDim) {
using af::constant;
using af::deviceGC;
using af::span;
// const int nx = 32;
const int nx = 1;
const int ny = 4 * 1024 * 1024;
const int nw = 4 * 1024 * 1024;
deviceGC();
{
array in = randu(nx, ny, u8);
array joined = join(0, in, in);
dim4 in_dims = in.dims();
dim4 joined_dims = joined.dims();
ASSERT_EQ(2 * in_dims[0], joined_dims[0]);
ASSERT_EQ(0.f, sum<float>((joined(0, span) - joined(1, span)).as(f32)));
array in2 = constant(1, (dim_t)nx, (dim_t)ny, (dim_t)2, (dim_t)nw, u8);
joined = join(3, in, in);
in_dims = in.dims();
joined_dims = joined.dims();
ASSERT_EQ(2 * in_dims[3], joined_dims[3]);
}
}
示例9: randu
TEST(Susan, InvalidThreshold) {
try {
array a = randu(256);
features out = susan(a, 3, -32, 10, 0.05f, 3);
EXPECT_TRUE(false);
} catch (exception &e) { EXPECT_TRUE(true); }
}
示例10: anisotropicDiffusion
TEST(AnisotropicDiffusion, GradientInvalidInputArray)
{
try {
array out = anisotropicDiffusion(randu(100), 0.125f, 0.2f, 10, AF_FLUX_QUADRATIC);
} catch (exception &exp) {
ASSERT_EQ(AF_ERR_SIZE, exp.err());
}
}
示例11: dims
TEST(Select, ISSUE_1249)
{
dim4 dims(2, 3, 4);
array cond = randu(dims) > 0.5;
array a = randu(dims);
array b = select(cond, a - a * 0.9, a);
array c = a - a * cond * 0.9;
int num = (int)dims.elements();
vector<float> hb(num);
vector<float> hc(num);
b.host(&hb[0]);
c.host(&hc[0]);
for (int i = 0; i < num; i++) {
EXPECT_NEAR(hc[i], hb[i], 1e-7) << "at " << i;
}
}
示例12: fftconvolveTestLarge
void fftconvolveTestLarge(int sDim, int fDim, int sBatch, int fBatch, bool expand)
{
if (noDoubleTests<T>()) return;
using af::dim4;
using af::seq;
using af::array;
int outDim = sDim + fDim - 1;
int fftDim = (int)pow(2, ceil(log2(outDim)));
int sd[4], fd[4];
for (int k = 0; k < 4; k++) {
if (k < baseDim) {
sd[k] = sDim;
fd[k] = fDim;
}
else if (k == baseDim) {
sd[k] = sBatch;
fd[k] = fBatch;
}
else {
sd[k] = 1;
fd[k] = 1;
}
}
const dim4 signalDims(sd[0], sd[1], sd[2], sd[3]);
const dim4 filterDims(fd[0], fd[1], fd[2], fd[3]);
array signal = randu(signalDims, (af_dtype) af::dtype_traits<T>::af_type);
array filter = randu(filterDims, (af_dtype) af::dtype_traits<T>::af_type);
array out = fftConvolve(signal, filter, expand ? AF_CONV_EXPAND : AF_CONV_DEFAULT);
array gold;
switch(baseDim) {
case 1:
gold = real(af::ifft(af::fft(signal, fftDim) * af::fft(filter, fftDim)));
break;
case 2:
gold = real(af::ifft2(af::fft2(signal, fftDim, fftDim) * af::fft2(filter, fftDim, fftDim)));
break;
case 3:
gold = real(af::ifft3(af::fft3(signal, fftDim, fftDim, fftDim) * af::fft3(filter, fftDim, fftDim, fftDim)));
break;
default:
ASSERT_LT(baseDim, 4);
}
int cropMin = 0, cropMax = 0;
if (expand) {
cropMin = 0;
cropMax = outDim - 1;
}
else {
cropMin = fDim/2;
cropMax = outDim - fDim/2 - 1;
}
switch(baseDim) {
case 1:
gold = gold(seq(cropMin, cropMax));
break;
case 2:
gold = gold(seq(cropMin, cropMax), seq(cropMin, cropMax));
break;
case 3:
gold = gold(seq(cropMin, cropMax), seq(cropMin, cropMax), seq(cropMin, cropMax));
break;
}
size_t outElems = out.elements();
size_t goldElems = gold.elements();
ASSERT_EQ(goldElems, outElems);
T *goldData = new T[goldElems];
gold.host(goldData);
T *outData = new T[outElems];
out.host(outData);
for (size_t elIter=0; elIter<outElems; ++elIter) {
ASSERT_NEAR(goldData[elIter], outData[elIter], 5e-2) << "at: " << elIter << std::endl;
}
delete[] goldData;
delete[] outData;
}
示例13: randu
TEST(Where, ISSUE_1259) {
array a = randu(10, 10, 10);
array indices = where(a > 2);
ASSERT_EQ(indices.elements(), 0);
}
示例14: TEST
TEST(FFTConvolve, Docs_Unified_Wrapper)
{
// This unit test doesn't necessarily need to function
// accuracy as af::convolve is merely a wrapper to
// af::convolve[1|2|3]
using af::array;
using af::dim4;
using af::randu;
using af::constant;
using af::convolve;
//![ex_image_convolve_1d]
array a = randu(10);
//af_print(a);
//a [10 1 1 1] = 0.0000 0.1315 0.7556 0.4587 0.5328 0.2190 0.0470 0.6789 0.6793 0.9347
array b = randu(4);
//af_print(b);
//b [4 1 1 1] = 0.3835 0.5194 0.8310 0.0346
array c = convolve(a, b);
//af_print(c);
//c [10 1 1 1] = 0.3581 0.6777 1.0750 0.7679 0.5903 0.4851 0.6598 1.2770 1.0734 0.8002
//![ex_image_convolve_1d]
//![ex_image_convolve_2d]
array d = constant(0.5, 5, 5);
//af_print(d);
//d [5 5 1 1]
// 0.5000 0.5000 0.5000 0.5000 0.5000
// 0.5000 0.5000 0.5000 0.5000 0.5000
// 0.5000 0.5000 0.5000 0.5000 0.5000
// 0.5000 0.5000 0.5000 0.5000 0.5000
// 0.5000 0.5000 0.5000 0.5000 0.5000
array e = constant(1, 2, 2);
//af_print(e);
//e [2 2 1 1]
// 1.0000 1.0000
// 1.0000 1.0000
array f = fftConvolve(d, e);
//af_print(f);
//f [5 5 1 1]
// 2.0000 2.0000 2.0000 2.0000 1.0000
// 2.0000 2.0000 2.0000 2.0000 1.0000
// 2.0000 2.0000 2.0000 2.0000 1.0000
// 2.0000 2.0000 2.0000 2.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000 0.5000
//![ex_image_convolve_2d]
//![ex_image_convolve_3d]
array g = constant(1, 4, 4, 4);
//af_print(g);
//g [4 4 4 1]
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
// 1.0000 1.0000 1.0000 1.0000
array h = constant(0.5, 2, 2, 2);
//af_print(h);
//h [2 2 2 1]
// 0.5000 0.5000
// 0.5000 0.5000
// 0.5000 0.5000
// 0.5000 0.5000
array i = fftConvolve(g, h);
//af_print(i);
//i [4 4 4 1]
// 4.0000 4.0000 4.0000 2.0000
// 4.0000 4.0000 4.0000 2.0000
// 4.0000 4.0000 4.0000 2.0000
// 2.0000 2.0000 2.0000 1.0000
// 4.0000 4.0000 4.0000 2.0000
// 4.0000 4.0000 4.0000 2.0000
// 4.0000 4.0000 4.0000 2.0000
// 2.0000 2.0000 2.0000 1.0000
// 4.0000 4.0000 4.0000 2.0000
// 4.0000 4.0000 4.0000 2.0000
// 4.0000 4.0000 4.0000 2.0000
// 2.0000 2.0000 2.0000 1.0000
// 2.0000 2.0000 2.0000 1.0000
// 2.0000 2.0000 2.0000 1.0000
// 2.0000 2.0000 2.0000 1.0000
//.........这里部分代码省略.........
示例15: randu
********************************************************/
#include <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <string>
#include <vector>
#include <testHelpers.hpp>
using std::vector;
using af::array;
using af::randu;
TEST(rgb_gray, 32bit)
{
array rgb = randu(10, 10, 3);
array gray = rgb2gray(rgb);
vector<float> h_rgb(rgb.elements());
vector<float> h_gray(gray.elements());
rgb.host(&h_rgb[0]);
gray.host(&h_gray[0]);
int num = gray.elements();
int roff = 0;
int goff = num;
int boff = 2 * num;
const float rPercent=0.2126f;
const float gPercent=0.7152f;