import unittest

import numpy

import chainer.functions as F
from chainer import testing


def make_data(dtype, shape):
    x = numpy.random.uniform(0.1, 5, shape).astype(dtype)
    gy = numpy.random.uniform(-1, 1, shape).astype(dtype)
    return x, gy


#
# sqrt

@testing.math_function_test(F.Sqrt(), make_data=make_data)
class TestSqrt(unittest.TestCase):
    pass


#
# rsqrt

def rsqrt(x, dtype=numpy.float32):
    return numpy.reciprocal(numpy.sqrt(x, dtype=dtype))


# TODO(takagi) Fix test of rsqrt not to use this decorator.
@testing.math_function_test(F.rsqrt, func_expected=rsqrt, make_data=make_data)
class TestRsqrt(unittest.TestCase):
    pass


testing.run_module(__name__, __file__)

import unittest

import numpy

import chainer.functions as F
from chainer import testing


#
# sqrt

def make_data(dtype, shape):
    x = numpy.random.uniform(0.1, 5, shape).astype(dtype)
    gy = numpy.random.uniform(-1, 1, shape).astype(dtype)
    return x, gy


@testing.math_function_test(F.Sqrt(), make_data=make_data)
class TestSqrt(unittest.TestCase):
    pass


#
# rsqrt

def rsqrt(x):
    return numpy.reciprocal(numpy.sqrt(x))


class TestRsqrt(unittest.TestCase):

    def test_rsqrt(self):
        x = numpy.random.uniform(0.1, 5, (3, 2)).astype(numpy.float32)
        testing.assert_allclose(F.rsqrt(x).data, rsqrt(x))


testing.run_module(__name__, __file__)

Simplify test of rsqrt function.
Simplify test of rsqrt function.

  import unittest
  
  import numpy
  
  import chainer.functions as F
  from chainer import testing
  
  
+ #
+ # sqrt
+ 
  def make_data(dtype, shape):
      x = numpy.random.uniform(0.1, 5, shape).astype(dtype)
      gy = numpy.random.uniform(-1, 1, shape).astype(dtype)
      return x, gy
  
- 
- #
- # sqrt
  
  @testing.math_function_test(F.Sqrt(), make_data=make_data)
  class TestSqrt(unittest.TestCase):
      pass
  
  
  #
  # rsqrt
  
- def rsqrt(x, dtype=numpy.float32):
+ def rsqrt(x):
-     return numpy.reciprocal(numpy.sqrt(x, dtype=dtype))
+     return numpy.reciprocal(numpy.sqrt(x))
  
  
- # TODO(takagi) Fix test of rsqrt not to use this decorator.
- @testing.math_function_test(F.rsqrt, func_expected=rsqrt, make_data=make_data)
  class TestRsqrt(unittest.TestCase):
-     pass
+ 
+     def test_rsqrt(self):
+         x = numpy.random.uniform(0.1, 5, (3, 2)).astype(numpy.float32)
+         testing.assert_allclose(F.rsqrt(x).data, rsqrt(x))
  
  
  testing.run_module(__name__, __file__)
  
Simplify test of rsqrt function.
## Code Before:
import unittest

import numpy

import chainer.functions as F
from chainer import testing


def make_data(dtype, shape):
    x = numpy.random.uniform(0.1, 5, shape).astype(dtype)
    gy = numpy.random.uniform(-1, 1, shape).astype(dtype)
    return x, gy


#
# sqrt

@testing.math_function_test(F.Sqrt(), make_data=make_data)
class TestSqrt(unittest.TestCase):
    pass


#
# rsqrt

def rsqrt(x, dtype=numpy.float32):
    return numpy.reciprocal(numpy.sqrt(x, dtype=dtype))


# TODO(takagi) Fix test of rsqrt not to use this decorator.
@testing.math_function_test(F.rsqrt, func_expected=rsqrt, make_data=make_data)
class TestRsqrt(unittest.TestCase):
    pass


testing.run_module(__name__, __file__)

## Instruction:
Simplify test of rsqrt function.
## Code After:
import unittest

import numpy

import chainer.functions as F
from chainer import testing


#
# sqrt

def make_data(dtype, shape):
    x = numpy.random.uniform(0.1, 5, shape).astype(dtype)
    gy = numpy.random.uniform(-1, 1, shape).astype(dtype)
    return x, gy


@testing.math_function_test(F.Sqrt(), make_data=make_data)
class TestSqrt(unittest.TestCase):
    pass


#
# rsqrt

def rsqrt(x):
    return numpy.reciprocal(numpy.sqrt(x))


class TestRsqrt(unittest.TestCase):

    def test_rsqrt(self):
        x = numpy.random.uniform(0.1, 5, (3, 2)).astype(numpy.float32)
        testing.assert_allclose(F.rsqrt(x).data, rsqrt(x))


testing.run_module(__name__, __file__)
