2018年7月24日火曜日

非負の可測関数の積分の定義に関する疑問

とある本を読んでいて,非負の可測関数の積分が以下で定義してあった.
$(\Omega, \mathcal{F}, \mu)$を測度空間とする. 関数$f: \Omega \rightarrow [0, \infty)$を$\mathcal{F}$-可測とする. $\{f_n\}$を,各点$x \in \Omega$で単調に増加する非負単関数列とする. $\lim_{n \rightarrow \infty} f_n(x) = f(x)$とする(各点収束). この時,$f$の積分を以下で定義する. $$ \begin{aligned} \int_\Omega f(x) d\mu(x) = \lim_{n \rightarrow \infty} \int_\Omega f_n(x) d\mu(x) \end{aligned} $$
これを見たとき,数列$\{\int_\Omega f_n(x) d\mu(x)\}$は収束するのだろうか?という疑問が浮かんだので,整理してみる. 有界単調増加数列の収束定理より,$\int_\Omega f_{n}(x) d\mu(x) \geq \int_\Omega f_{n-1}(x) d\mu(x)$を示せば良い. $f_n - f_{n-1}$も単関数である. そして,$x \in \Omega$で,$f_n(x) - f_{n-1}(x) \geq 0$である. $f_n(x) - f_{n-1}(x) = \sum_{j=1}^m \alpha_j 1_{B_j}(x)$とすると,$\alpha_j \geq 0$でなければならない.なぜなら,単関数の定義より,$B_j$は互いに素なので,$x$はどれかひとつの$B_j$にしか属することでできない.つまり,どれか一つの$\alpha_j$が選ばれることになる.ということわけで,$\alpha_j \geq 0, \ \forall j = 1, \ldots, m$でなければならない.単関数の積分の定義より, $$ \begin{aligned} \int_{\Omega} (f_n(x) - f_{n-1}(x)) d\mu(x) = \sum_{j=1}^m \alpha_j \mu(B_j) \geq 0 \\ \end{aligned} $$ となる.よって, $$ \begin{aligned} &\int_{\Omega} (f_n(x) - f_{n-1}(x)) d\mu(x) \geq 0 \\ &\Leftrightarrow \int_{\Omega} f_n(x) d\mu(x) - \int_{\Omega} f_{n-1}(x) d\mu(x) \geq 0 \text{(要証明)} \\ &\Leftrightarrow \int_{\Omega} f_n(x) d\mu(x) \geq \int_{\Omega} f_{n-1}(x) d\mu(x) \end{aligned} $$ となる.したがって,$\{\int_\Omega f_n(x) d\mu(x)\}$は収束する. > Written with [StackEdit](https://stackedit.io/).

2017年2月1日水曜日

ニュートン法


が凸だとすると,この2次近似も凸である.この近似式のヘッシアンはなので,これが半正定値であればこの2次近似は凸.今,が凸なので,そのヘッシアンは半正定値行列.よってこの2次近似は凸.
この2次近似は凸なので,となる点がグローバルな最適解.これはFirst-order conditionから言える.以下のように求められる.

つまり,と更新すれば良い.

ざっくり理解.

EquationTest

以下の点xにおける1次近似を最小化する方向vを求める.

最適化問題:


where .

ここから,とわかる.

Written with StackEdit.

2014年1月9日木曜日

How do we give objects from the server to the client-side? python.flask

related to Python, Flask, JQuery, Javascript.

this image is created by keyart (http://nktyasu.dip.jp/keyart)
This image is created by keyart (http://nktyasu.dip.jp/keyart)

When you make a web application, you may face this problem. In the most of applications, the server gives objects to client-side (like javascript). In this article, I’d like to show a way using JSON.
Note that we use Python and Flask, but you can apply this any other Programming languages and frameworks.

Making API

First, you should make your own API (Application Programming Interface). Sample code is like this:

from flask import Flask
import json
app = Flask(__name__)

@app.route("/get_data")
def get_data():
    data = [{"firstname": "hiroyuki", "lastname": "tanaka"},
            {"firstname": "John", "lastname": "Smith"}]
    return json.dumps({"data": data})

The get_data method return a JSON object as a text. Let’s run the development server:

$ python demo.py

and access to localhost:5000/get_data. you will see the object. Now you are ready for getting objects.

Call the API from client-side using Ajax

This is the key of this article. Since your API gives the client objects, your client have to receive the objects. To do this, we use Ajax. Sample code is like this:

$(function(){
    var data;
    $.ajax({
        type: "GET",
        url: $SCRIPT_ROOT + "/get_data",
        dataType: "json",
        timeout: 120000,
        error: errorHandlier,
        success: function(r){
            data = r.data;
        }
    });
    setTimeout(function(){
        if(data != null){
            /* whatever you want to do using the results */
            /* For example using above API, */ 
            alert("Hello "+data[0].firstname+" "+data[0].lastname);
        }
        else{
            setTimeout(arguments.callee, 100);
        }
    }
});

And you have to set $SCRIPT_ROOT in your html file like this:

<script> $SCRIPT_ROOT = {{ request.script_root|tojson|safe }}; </script>

As you can see in the code, we should use setTimeout method to wait until loading the object completely since The $.ajax method is asynchronous.

That’s it. In the rest of article, I want to discuss about how to give objects from the server to client.

Discussion

using template engine ( flask.render_template)

Flask uses powerful template engine (jinja2?). You can give objects using this. the server side script is like this:

from flask import Flask, render_template
import json
app = Flask(__name__)

@app.route("/get_data")
def get_data():
    data = [{"firstname": "hiroyuki", "lastname": "tanaka"},
            {"firstname": "John", "lastname": "Smith"}]
    return render_template("index.html", data=json.dumps({"data": data}))

and index.html is like this:

<html>
<head>
<script> data = {{data|safe}} </script>
</head>

That’s it. This is very easy. I used to use this. But users can see the objects. So if you don’t matter that users can see the objects, you use this way.

using $.getJson method

You can use $.getJson method instead of $.ajax method like this:

var data
    $.getJSON($SCRIPT_ROOT+"/get_data", function(r){
            data = r.data;
    });

But, when I tried this, it doesn’t work well on iPhone Safari.

Any suggestions?

Thank you for reading this article. I mentioned how we give obejcts from the server to client. But I’m almost new about web development. If you have more useful and clever way, Please let me know.

Written with StackEdit.

make_response

Vukasin-san proposed a better way using make_response. Thanks for helping^^.
I wrote sample code like this:

from flask import Flask, make_response
import ujson
app = Flask(__name__)

@app.route("/get_objects")
def get_objects():
    data = [{"firstname": "hiroyuki", "lastname": "tanaka"},
            {"firstname": "John", "lastname": "Smith"}]
    response = make_response()
    response.headers["Content-Type"] = "application/json"
    response.status_code = 200
    response.data = {"data": ujson.dumps(data)}
    return response