コード例 #1
0
ファイル: lazy.py プロジェクト: roed314/padicprec
    def __getattribute__(self, name):
        instance = LazyApproximation.__getattribute__(self, "_instance")
        attr = instance.__getattribute__(name)
        try:
            vars = inspect.getargspec(attr)[0]
            vars.index("wrapper")
        except (TypeError, ValueError):
            return attr
        ref = Approximation.__getattribute__(self, "_ref")

        def modified_attr(*args, **kwargs):
            kwargs["wrapper"] = ref
            return attr(*args, **kwargs)

        return modified_attr
コード例 #2
0
ファイル: lazy.py プロジェクト: roed314/padicprec
    def __init__(self, parent, x, max_workprec=None, **kwargs):
        Approximation.__init__(self, parent)
        self._length = -1
        self._current_workprec = None
        self._current_approximation = None
        self._dict_max_workprec = {}
        if isinstance(x, Approximation) and x.parent() is parent:
            if isinstance(x, LazyApproximation):
                raise TypeError("can't lazy something already lazy")
            self._current_workprec = Infinity
            self._current_approximation = x
            self.repr = lambda: "%s" % x
            self._length = length = x._length
            base = parent.base_ring()
            if length >= 0:
                zero = base._lazy_zero
                lazy_class = base._lazy_class

                @cached_function
                def getitem_by_num(i):
                    if i < length:
                        return lazy_class(base, x._getitem_by_num(i)).new_wrapper()
                    else:
                        return zero

                self._getitem_by_num = getitem_by_num
        elif isinstance(x, list):
            self.name = "element_from_list"
            if parent.dimension() < Infinity:
                zeros = [parent.base_ring()._zero] * (parent.dimension() - len(x))
                self.function = lambda workprec: parent([item.at_workprec(workprec) for item in x] + zeros)
            else:
                self.function = lambda workprec: parent([item.at_workprec(workprec) for item in x])
            x_wrapper = [c.new_wrapper() for c in x]
            self._length = length = len(x)
            zero = parent.base_ring()._lazy_zero

            def getitem_by_num(i):
                if i < length:
                    return x_wrapper[i]
                else:
                    return zero

            self._getitem_by_num = getitem_by_num
            self.args = self._dependances = x_wrapper
            if len(x) == 0:
                if max_workprec is None:
                    max_workprec = 0
            else:
                if max_workprec is None:
                    max_workprec = max([item.max_workprec() for item in x])
        elif isinstance(x, dict):
            raise NotImplementedError
        elif isinstance(x, FunctionType):
            self.function = x
            import re

            self.name = re.sub("^(lazy)?_*", "", x.__name__)
            self.name = re.sub("_*$", "", self.name)
        else:
            raise TypeError("x must be an approximation, a list, a dictionary or a function")

        if max_workprec is None:
            self._max_workprec = Infinity
        else:
            self._max_workprec = max_workprec

        if kwargs.has_key("parenthesis_level"):
            self._parenthesis_level = kwargs["parenthesis_level"]
            del kwargs["parenthesis_level"]
        else:
            self._parenthesis_level = 3
            try:
                if self.name == "add" or self.name == "sub" or self.name == "neg":
                    self._parenthesis_level = 0
                elif self.name == "mul" or self.name == "div" or self.name == "invert":
                    self._parenthesis_level = 1
                elif self.name == "pow":
                    self._parenthesis_level = 2
            except AttributeError:
                pass

        for key in kwargs:
            if key[:1] != "_":
                self.__dict__[key] = kwargs[key]
コード例 #3
0
Approximation.verbose_learn = False
Approximation.verbose_probe = False

#input_range = np.array([-3.5, 3.5])
#output_range = np.array([0.0, fn_square(input_range[0])])
input_range = np.array([(-1.0, 1.0), (-1.0, 1.0)])
output_range = np.array([-2.0, 2.0])

print("input_range: {} output_range: {}".format(input_range, output_range))

#HRR.incremental_weight = 0.5

#appr = Approximation(fn=fn_2dparab, size=3000)
#appr.learn(input_range=input_range, output_range=output_range, n_samples=200, stddev=0.02, use_incremental=False)
#appr.plot_result(input_range=input_range, output_range=output_range, n_samples=19)
appr = Approximation(fn=fn_plane, size=10000)
appr.learn(input_range=input_range,
           output_range=output_range,
           n_samples=(10, 10),
           stddev=0.01,
           use_incremental=False)

n_samples = (10, 10)

if True:
    input_tuples = []
    X = np.linspace(-1.0, 1.0, n_samples[0])
    Y = np.linspace(-1.0, 1.0, n_samples[1])
    for x in X:
        for y in Y:
            input_tuples.append((x, y))
コード例 #4
0
HRR.verbose = False

armlengths = [1.0, 1.0]


def fn(x, y):
    tcp = TCP([x, y], arms=2, armlengths=armlengths)
    tcp.computeTcp()
    return tcp.tcp  # should be list of length 3


input_range = [(0.0, 360.0), (0.0, 360.0)]
sr = np.sum(armlengths)
output_range = [(-sr, sr), (-sr, sr)]

appr = Approximation(fn=fn, size=3000)
appr.learn(input_range=input_range,
           output_range=output_range,
           n_samples=(20, 20),
           stddev=0.02,
           use_incremental=False)

input_tuples = []
S = np.linspace(0.0, 360.0, 10)
for joint1 in S:
    for joint2 in S:
        input_tuples.append((joint1, joint2))
appr.verify(input_tuples=input_tuples,
            input_range=input_range,
            output_range=output_range)
コード例 #5
0
 def __init__(self):
     self.approximation = Approximation()
コード例 #6
0
name = "Y=X²-6"

if __name__ == '__main__':

    ys = []
    num = 0
    for x in range(-10, 11):
        if random.randint(1, 100) < 60:
            temp = [x]
            xs.append([])
            xs[num] = (temp)
            ys += [x * x - 6]
            num += 1
    for degree in range(2, 13):

        approximation = Approximation.fit(xs, ys, degree)

        graph = Graph(xs, ys, approximation, degree, name)
        graph.draw()

    data = {
        "data": {
            "coef": approximation.coef,
            "names": approximation.names,
            "json": approximation.json,
            "latex": approximation.latex,
        }
    }
    with open('data.json', 'w') as outfile:
        json.dump(data, outfile)