#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# written by Shotaro Fujimoto
# 2016-08-19

import scipy.optimize as optimize
import numpy as np


class Optimize_powerlaw:
    def __init__(self, args, parameters):
        self.args = args
        self.parameters = parameters

    def func(self, parameters, x, y):
        c = parameters[0]
        D = parameters[1]
        # residual = y - c * (x ** D)
        residual = np.log(y) - np.log(c) - D * np.log(x)
        return residual

    def fitting(self):
        result = optimize.leastsq(self.func, x0=self.parameters, args=self.args)
        self.c = result[0][0]
        self.D = result[0][1]
        return {'c': self.c, 'D': self.D}

    def fitted(self, x):
        return self.c * (x ** self.D)

class Optimize_linear:
    def __init__(self, args, parameters):
        self.args = args
        self.parameters = parameters

    def func(self, parameters, x, y):
        a = parameters[0]
        b = parameters[1]
        residual = y - (a * x + b)
        return residual

    def fitting(self):
        result = optimize.leastsq(self.func, x0=self.parameters, args=self.args)
        self.a = result[0][0]
        self.b = result[0][1]
        return {'a': self.a, 'b': self.b}

    def fitted(self, x):
        return self.a * x + self.b
