Example #1
0
def norm(x: ndarray,
         ord: tp.Optional[int] = None,
         axis: tp.Optional[int] = None,
         keepdims: bool = False) -> float:
    """
    Matrix or vector norm.
    """

    if axis is not None:
        raise ValueError("axis != None is not supported")

    p = 1.0
    q = 1.0

    if ord == 2:
        S = svd(x, compute_uv=False)
        return S.max()
    else:
        if ord is None:
            norm_type = af.NORM.VECTOR_2
        elif isinstance(ord, Number):
            if ord == float("inf") or ord == np.inf:
                norm_type = af.NORM.MATRIX_INF
            elif ord == 1:
                norm_type = af.NORM.MATRIX_1
            else:
                norm_type = af.NORM.VECTOR_P
                p = ord
        else:
            raise ValueError(f"ord = {ord} is not supported")

        return af.norm(x._af_array, norm_type=norm_type, p=p, q=q)
Example #2
0
def simple_lapack(verbose=False):
    display_func = _util.display_func(verbose)
    print_func = _util.print_func(verbose)
    a = af.randu(5, 5)

    l, u, p = af.lu(a)

    display_func(l)
    display_func(u)
    display_func(p)

    p = af.lu_inplace(a, "full")

    display_func(a)
    display_func(p)

    a = af.randu(5, 3)

    q, r, t = af.qr(a)

    display_func(q)
    display_func(r)
    display_func(t)

    af.qr_inplace(a)

    display_func(a)

    a = af.randu(5, 5)
    a = af.matmulTN(a, a.copy()) + 10 * af.identity(5, 5)

    R, info = af.cholesky(a)
    display_func(R)
    print_func(info)

    af.cholesky_inplace(a)
    display_func(a)

    a = af.randu(5, 5)
    ai = af.inverse(a)

    display_func(a)
    display_func(ai)

    x0 = af.randu(5, 3)
    b = af.matmul(a, x0)
    x1 = af.solve(a, b)

    display_func(x0)
    display_func(x1)

    p = af.lu_inplace(a)

    x2 = af.solve_lu(a, p, b)

    display_func(x2)

    print_func(af.rank(a))
    print_func(af.det(a))
    print_func(af.norm(a, af.NORM.EUCLID))
    print_func(af.norm(a, af.NORM.MATRIX_1))
    print_func(af.norm(a, af.NORM.MATRIX_INF))
    print_func(af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1))

    a = af.randu(10, 10)
    display_func(a)
    u, s, vt = af.svd(a)
    display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt))
    u, s, vt = af.svd_inplace(a)
    display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt))
Example #3
0
def simple_lapack(verbose=False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)
    a = af.randu(5,5)

    l,u,p = af.lu(a)

    display_func(l)
    display_func(u)
    display_func(p)

    p = af.lu_inplace(a, "full")

    display_func(a)
    display_func(p)

    a = af.randu(5,3)

    q,r,t = af.qr(a)

    display_func(q)
    display_func(r)
    display_func(t)

    af.qr_inplace(a)

    display_func(a)

    a = af.randu(5, 5)
    a = af.matmulTN(a, a) + 10 * af.identity(5,5)

    R,info = af.cholesky(a)
    display_func(R)
    print_func(info)

    af.cholesky_inplace(a)
    display_func(a)

    a = af.randu(5,5)
    ai = af.inverse(a)

    display_func(a)
    display_func(ai)

    x0 = af.randu(5, 3)
    b = af.matmul(a, x0)
    x1 = af.solve(a, b)

    display_func(x0)
    display_func(x1)

    p = af.lu_inplace(a)

    x2 = af.solve_lu(a, p, b)

    display_func(x2)

    print_func(af.rank(a))
    print_func(af.det(a))
    print_func(af.norm(a, af.NORM.EUCLID))
    print_func(af.norm(a, af.NORM.MATRIX_1))
    print_func(af.norm(a, af.NORM.MATRIX_INF))
    print_func(af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1))
print(info)

af.cholesky_inplace(a)
af.display(a)

a = af.randu(5, 5)
ai = af.inverse(a)

af.display(a)
af.display(ai)

x0 = af.randu(5, 3)
b = af.matmul(a, x0)
x1 = af.solve(a, b)

af.display(x0)
af.display(x1)

p = af.lu_inplace(a)

x2 = af.solve_lu(a, p, b)

af.display(x2)

print(af.rank(a))
print(af.det(a))
print(af.norm(a, af.AF_NORM_EUCLID))
print(af.norm(a, af.AF_NORM_MATRIX_1))
print(af.norm(a, af.AF_NORM_MATRIX_INF))
print(af.norm(a, af.AF_NORM_MATRIX_L_PQ, 1, 1))
Example #5
0
m = n = None

k = 1  # timing iterations

phase_unwrapped = unwrap_dct(phase_wrapped)  # warm-up
af.device.sync()
t0 = time()
for i in range(k):
    phase_unwrapped = unwrap_dct(phase_wrapped)
af.device.sync()
t1 = time() - t0
af.device.device_gc()
coord = None
print('time:\t%fs' % (t1 / k))

print('norm:\t%f' % af.norm(phase_unwrapped - phase_orig))

fig, ((ax1, ax2), (ax3, ax4)) = pl.subplots(2, 2)
im1 = ax1.imshow(phase_orig.__array__())
pl.colorbar(im1, ax=ax1)
ax1.set_title('original phase, SNR=%i' % snr)
im2 = ax2.imshow(phase_wrapped.__array__())
pl.colorbar(im2, ax=ax2)
ax2.set_title('wrapped phase')
im3 = ax3.imshow(phase_unwrapped.__array__())
pl.colorbar(im3, ax=ax3)
ax3.set_title('unwrapped phase')
im4 = ax4.imshow((phase_unwrapped - phase_orig).__array__())
pl.colorbar(im4, ax=ax4)
ax4.set_title('difference')
pl.show()
 def computeCost(self, x):
     return af.norm(af.moddims(x, np.prod(x.shape)),
                    norm_type=af.NORM.VECTOR_1)