#!/usr/bin/env python
# coding: utf-8

# # Newton's method in $n$ dimensions
# 
# Copyright (C) 2020 Andreas Kloeckner
# 
# <details>
# <summary>MIT License</summary>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# </details>

# In[2]:


import numpy as np
import numpy.linalg as la


# In[3]:


def f(xvec):
    x, y = xvec
    return np.array([
        x + 2*y -2,
        x**2 + 4*y**2 - 4
        ])



# In[4]:


def Jf(xvec):
    x, y = xvec
    return np.array([
        [1, 2],
        [2*x, 8*y]
        ])


# Pick an initial guess.

# In[5]:


x = np.array([1, 2])


# Now implement Newton's method.

# In[6]:


x = x - la.solve(Jf(x), f(x))
print(x)


# Check if that's really a solution:

# In[7]:


f(x)


# * What's the cost of one iteration?
# * Is there still something like quadratic convergence?

# --------------------
# Let's keep an error history and check.

# In[8]:


xtrue = np.array([0, 1])
errors = []
x = np.array([1, 2])


# In[19]:


x = x - la.solve(Jf(x), f(x))
errors.append(la.norm(x-xtrue))
print(x)


# In[20]:


for e in errors:
    print(e)


# In[21]:


for i in range(len(errors)-1):
    print(errors[i+1]/errors[i]**2)


# In[ ]:




