Python autograd.numpy.power() Examples

The following are 30 code examples of autograd.numpy.power(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module autograd.numpy , or try the search function .
Example #1
Source File: optimizer.py    From tinyik with MIT License 6 votes vote down vote up
def optimize(self, angles0, target):
        """Calculate an optimum argument of an objective function."""
        def new_objective(angles):
            a = angles - angles0
            if isinstance(self.smooth_factor, (np.ndarray, list)):
                if len(a) == len(self.smooth_factor):
                    return (self.f(angles, target) +
                            np.sum(self.smooth_factor * np.power(a, 2)))
                else:
                    raise ValueError('len(smooth_factor) != number of joints')
            else:
                return (self.f(angles, target) +
                        self.smooth_factor * np.sum(np.power(a, 2)))

        return scipy.optimize.minimize(
            new_objective,
            angles0,
            **self.optimizer_opt).x 
Example #2
Source File: optimizer.py    From Robotic_Manipulation with MIT License 6 votes vote down vote up
def optimize(self, angles0, target):
        """Calculate an optimum argument of an objective function."""
        def new_objective(angles):
            a = angles - angles0
            if isinstance(self.smooth_factor, (np.ndarray, list)):
                if len(a) == len(self.smooth_factor):
                    return (self.f(angles, target) +
                            np.sum(self.smooth_factor * np.power(a, 2)))
                else:
                    raise ValueError('len(smooth_factor) != number of joints')
            else:
                return (self.f(angles, target) +
                        self.smooth_factor * np.sum(np.power(a, 2)))

        return scipy.optimize.minimize(
            new_objective,
            angles0,
            **self.optimizer_opt).x 
Example #3
Source File: geometry.py    From AeroSandbox with MIT License 6 votes vote down vote up
def J(self):
        # Returns the nondimensionalized polar moment of inertia, taken about the centroid.
        x = self.coordinates[:, 0]
        y = self.coordinates[:, 1]
        x_n = np.roll(x, -1)  # x_next, or x_i+1
        y_n = np.roll(y, -1)  # y_next, or y_i+1

        a = x * y_n - x_n * y  # a is the area of the triangle bounded by a given point, the next point, and the origin.

        A = 0.5 * np.sum(a)  # area

        x_c = 1 / (6 * A) * np.sum(a * (x + x_n))
        y_c = 1 / (6 * A) * np.sum(a * (y + y_n))
        centroid = np.array([x_c, y_c])

        Ixx = 1 / 12 * np.sum(a * (np.power(y, 2) + y * y_n + np.power(y_n, 2)))

        Iyy = 1 / 12 * np.sum(a * (np.power(x, 2) + x * x_n + np.power(x_n, 2)))

        J = Ixx + Iyy

        return J 
Example #4
Source File: geometry.py    From AeroSandbox with MIT License 6 votes vote down vote up
def Iyy(self):
        # Returns the nondimensionalized Iyy moment of inertia, taken about the centroid.
        x = self.coordinates[:, 0]
        y = self.coordinates[:, 1]
        x_n = np.roll(x, -1)  # x_next, or x_i+1
        y_n = np.roll(y, -1)  # y_next, or y_i+1

        a = x * y_n - x_n * y  # a is the area of the triangle bounded by a given point, the next point, and the origin.

        A = 0.5 * np.sum(a)  # area

        x_c = 1 / (6 * A) * np.sum(a * (x + x_n))
        y_c = 1 / (6 * A) * np.sum(a * (y + y_n))
        centroid = np.array([x_c, y_c])

        Iyy = 1 / 12 * np.sum(a * (np.power(x, 2) + x * x_n + np.power(x_n, 2)))

        Ivv = Iyy - A * centroid[0] ** 2

        return Ivv 
Example #5
Source File: geometry.py    From AeroSandbox with MIT License 6 votes vote down vote up
def Ixx(self):
        # Returns the nondimensionalized Ixx moment of inertia, taken about the centroid.
        x = self.coordinates[:, 0]
        y = self.coordinates[:, 1]
        x_n = np.roll(x, -1)  # x_next, or x_i+1
        y_n = np.roll(y, -1)  # y_next, or y_i+1

        a = x * y_n - x_n * y  # a is the area of the triangle bounded by a given point, the next point, and the origin.

        A = 0.5 * np.sum(a)  # area

        x_c = 1 / (6 * A) * np.sum(a * (x + x_n))
        y_c = 1 / (6 * A) * np.sum(a * (y + y_n))
        centroid = np.array([x_c, y_c])

        Ixx = 1 / 12 * np.sum(a * (np.power(y, 2) + y * y_n + np.power(y_n, 2)))

        Iuu = Ixx - A * centroid[1] ** 2

        return Iuu 
Example #6
Source File: dtlz.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        X_, X_M = x[:, :self.n_obj - 1], x[:, self.n_obj - 1:]
        g = anp.sum(anp.power(X_M, 0.1), axis=1)

        theta = 1 / (2 * (1 + g[:, None])) * (1 + 2 * g[:, None] * X_)
        theta = anp.column_stack([x[:, 0], theta[:, 1:]])

        out["F"] = self.obj_func(theta, g) 
Example #7
Source File: test_scalar_ops.py    From autograd with MIT License 5 votes vote down vote up
def test_power_arg1():
    x = npr.randn()**2
    fun = lambda y : np.power(x, y)
    check_grads(fun)(npr.rand()**2) 
Example #8
Source File: test_scalar_ops.py    From autograd with MIT License 5 votes vote down vote up
def test_power_arg1_zero():
    fun = lambda y : np.power(0., y)
    check_grads(fun)(npr.rand()**2) 
Example #9
Source File: zakharov.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        a = np.sum(0.5 * np.arange(1, self.n_var + 1) * x, axis=1)
        out["F"] = np.sum(np.square(x), axis=1) + np.square(a) + np.power(a, 4) 
Example #10
Source File: cdtlz.py    From pymop with Apache License 2.0 5 votes vote down vote up
def constraint_c4_cylindrical(f, r):  # cylindrical
    l = anp.mean(f, axis=1)
    l = anp.expand_dims(l, axis=1)
    g = -anp.sum(anp.power(f - l, 2), axis=1) + anp.power(r, 2)
    return g 
Example #11
Source File: dtlz.py    From pymop with Apache License 2.0 5 votes vote down vote up
def obj_func(self, X_, g, alpha=1):
        f = []

        for i in range(0, self.n_obj):
            _f = (1 + g)
            _f *= anp.prod(anp.cos(anp.power(X_[:, :X_.shape[1] - i], alpha) * anp.pi / 2.0), axis=1)
            if i > 0:
                _f *= anp.sin(anp.power(X_[:, X_.shape[1] - i], alpha) * anp.pi / 2.0)

            f.append(_f)

        f = anp.column_stack(f)
        return f 
Example #12
Source File: methods.py    From tf-quant-finance with Apache License 2.0 5 votes vote down vote up
def non_uniform_errors(f, num, ndim, label=None):
  """Build DataFrame of approximation errors with non uniform grid."""
  points = non_uniform_grid(np.power(num, ndim), ndim)
  values = f(points)
  approx_all = non_uniform_approx_nearest(points, values)

  def name_errors():
    for (ds, name), approx in zip(derivative_names(ndim), approx_all.T):
      actual = autograd(f, ds, points)
      yield name, np.abs(actual - approx)

  return _build_errors_df(name_errors(), label) 
Example #13
Source File: dtlz.py    From pymop with Apache License 2.0 5 votes vote down vote up
def get_scale(n, scale_factor):
        return anp.power(anp.full(n, scale_factor), anp.arange(n)) 
Example #14
Source File: dtlz.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _calc_pareto_front(self, ref_dirs, *args, **kwargs):
        F = self.problem.pareto_front(ref_dirs)
        return anp.power(F, ConvexProblem.get_power(self.n_obj)) 
Example #15
Source File: rastrigin.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        z = anp.power(x, 2) - self.A * anp.cos(2 * anp.pi * x)
        out["F"] = self.A * self.n_var + anp.sum(z, axis=1) 
Example #16
Source File: zdt.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        f1 = x[:, 0]
        g = 1 + 9.0 / (self.n_var - 1) * anp.sum(x[:, 1:], axis=1)
        f2 = g * (1 - anp.power((f1 / g), 0.5))

        out["F"] = anp.column_stack([f1, f2]) 
Example #17
Source File: zdt.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _calc_pareto_front(self, n_pareto_points=100):
        x = anp.linspace(0, 1, n_pareto_points)
        return anp.array([x, 1 - anp.power(x, 2)]).T 
Example #18
Source File: zdt.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        f1 = x[:, 0]
        c = anp.sum(x[:, 1:], axis=1)
        g = 1.0 + 9.0 * c / (self.n_var - 1)
        f2 = g * (1 - anp.power((f1 * 1.0 / g), 2))

        out["F"] = anp.column_stack([f1, f2]) 
Example #19
Source File: zdt.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _calc_pareto_front(self, n_pareto_points=100):
        x = anp.linspace(0.2807753191, 1, n_pareto_points)
        return anp.array([x, 1 - anp.power(x, 2)]).T 
Example #20
Source File: zdt.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        f1 = 1 - anp.exp(-4 * x[:, 0]) * anp.power(anp.sin(6 * anp.pi * x[:, 0]), 6)
        g = 1 + 9.0 * anp.power(anp.sum(x[:, 1:], axis=1) / (self.n_var - 1.0), 0.25)
        f2 = g * (1 - anp.power(f1 / g, 2))

        out["F"] = anp.column_stack([f1, f2]) 
Example #21
Source File: define_custom_problem_with_gradient.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        f1 = x[:, 0]
        g = 1 + 9.0 / (self.n_var - 1) * np.sum(x[:, 1:], axis=1)
        f2 = g * (1 - np.power((f1 / g), 0.5))

        out["F"] = np.column_stack([f1, f2])

        if "dF" in out:
            dF = np.zeros([x.shape[0], self.n_obj, self.n_var], dtype=np.float)
            dF[:, 0, 0], dF[:, 0, 1:] = 1, 0
            dF[:, 1, 0] = -0.5 * np.sqrt(g / x[:, 0])
            dF[:, 1, 1:] = ((9 / (self.n_var - 1)) * (1 - 0.5 * np.sqrt(x[:, 0] / g)))[:, None]
            out["dF"] = dF 
Example #22
Source File: define_custom_problem_with_gradient.py    From pymop with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        f1 = x[:, 0]
        g = 1 + 9.0 / (self.n_var - 1) * anp.sum(x[:, 1:], axis=1)
        f2 = g * (1 - anp.power((f1 / g), 0.5))

        out["F"] = anp.column_stack([f1, f2]) 
Example #23
Source File: zdt.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def _calc_pareto_front(self, n_pareto_points=100):
        x = anp.linspace(0.2807753191, 1, n_pareto_points)
        return anp.array([x, 1 - anp.power(x, 2)]).T 
Example #24
Source File: dtlz.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def obj_func(self, X_, g, alpha=1):
        f = []

        for i in range(0, self.n_obj):
            _f = (1 + g)
            _f *= anp.prod(anp.cos(anp.power(X_[:, :X_.shape[1] - i], alpha) * anp.pi / 2.0), axis=1)
            if i > 0:
                _f *= anp.sin(anp.power(X_[:, X_.shape[1] - i], alpha) * anp.pi / 2.0)

            f.append(_f)

        f = anp.column_stack(f)
        return f 
Example #25
Source File: dtlz.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        X_, X_M = x[:, :self.n_obj - 1], x[:, self.n_obj - 1:]
        g = anp.sum(anp.power(X_M, 0.1), axis=1)

        theta = 1 / (2 * (1 + g[:, None])) * (1 + 2 * g[:, None] * X_)
        theta = anp.column_stack([x[:, 0], theta[:, 1:]])

        out["F"] = self.obj_func(theta, g) 
Example #26
Source File: dtlz.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def get_scale(n, scale_factor):
        return anp.power(anp.full(n, scale_factor), anp.arange(n)) 
Example #27
Source File: dtlz.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, X, out, *args, **kwargs):
        self.problem._evaluate(X, out, **kwargs)
        out["F"] = anp.power(out["F"], self.get_power(self.n_obj)) 
Example #28
Source File: zakharov.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        a = anp.sum(0.5 * anp.arange(1, self.n_var + 1) * x, axis=1)
        out["F"] = anp.sum(anp.square(x), axis=1) + anp.square(a) + anp.power(a, 4) 
Example #29
Source File: rastrigin.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        z = anp.power(x, 2) - self.A * anp.cos(2 * anp.pi * x)
        out["F"] = self.A * self.n_var + anp.sum(z, axis=1) 
Example #30
Source File: zdt.py    From pymoo with Apache License 2.0 5 votes vote down vote up
def _evaluate(self, x, out, *args, **kwargs):
        f1 = x[:, 0]
        g = 1 + 9.0 / (self.n_var - 1) * anp.sum(x[:, 1:], axis=1)
        f2 = g * (1 - anp.power((f1 / g), 0.5))

        out["F"] = anp.column_stack([f1, f2])