6.02.2014

IVA Reading: Chapter 01, Section 05 Exercises

IVA読書会 chap01-sect05 宿題

 

No.5

イデアルの基底から左辺右辺のお互いの多項式が生成できることを示す。

$ \forall q \in k[x], \\ 1\cdot(f-qg) + q\cdot g = f \\ 0\cdot(f-qg) + 1\cdot g = g $

より、 $\langle f - qg, g \rangle \supset \langle f, g \rangle$

また、

$ \forall q \in k[x], \\ 1\cdot f + (-q)\cdot g = f - qg \\ 0\cdot f + 1\cdot g = g $

より、
$\langle f - qg, g \rangle \subset \langle f, g \rangle$

従って、$\forall q \in k[x], \langle f - qg, g \rangle = \langle f, g \rangle$

□




 

No.11

a)

セクション1 定理7: あらゆる定数でない多項式 $f \in \mathbb{C}[x] $ は $\mathbb{C}$ 上に根を持つ

より、
$f \text{ is nonconstant polynomial} \Rightarrow \text{deg}(f) > 0 \Rightarrow {\bf V}(f) \ne \emptyset$

対偶を取ると、
${\bf V}(f) = \emptyset \Rightarrow f \text{ is constant}$

一方、明らかに
$f \text{ is constant} \Rightarrow \text{deg}(f) = 0 \Rightarrow {\bf V}(f) = \emptyset$




従って
${\bf V}(f) = \emptyset \Leftrightarrow f \text{ is constant}$

 

b)
${\bf V}(f_1,...,f_s) \ne \emptyset$ であるならば、$f_1 = \cdots = f_s = 0$ は$x$について1個以上の解を得る。

その解の一つを $x=a$ とすれば、$f_1,...,f_s$ は全て $(x-a)$ で割り切れるため、$\text{GCD}(f_1,...,f_s) \ne 1$ ...(1)

また、$\text{GCD}(f_1,...,f_s) \ne 1$ である場合を考える。

$\text{GCD}(f_1,...,f_s) = g$ とおくと、
${\bf V}(f_1,...,f_s) = {\bf V}(\text{GCD}(f_1,...,f_s)) = {\bf V}(g)$ と書ける。
$g$ は非定数であるから前問の対偶から、${\bf V}(f_1,...,f_s) = {\bf V}(g) \ne \emptyset$ ...(2)

(1), (2) の対偶を取れば
${\bf V}(f_1,...,f_s) = \emptyset \Leftrightarrow \text{GCD}(f_1,...,f_s)=1$

c)
前問より

$\text{GCD}(f_1,...,f_s) = 1 \Rightarrow {\bf V}(f_1,...,f_s) = \emptyset$

$\text{GCD}(f_1,...,f_s) \ne 1 \Rightarrow {\bf V}(f_1,...,f_s) \text{ is nonempty}$




 

No.17

$\begin{eqnarray*} & & {\bf I}({\bf V}(x^5 - 2x^4 + 2x^2 - x, x^5 - x^4 - 2x^3 + 2x^2 + x - 1)) \\ &=& {\bf I}({\bf V}(\text{GCD}(x^5 - 2x^4 + 2x^2 - x, x^5 - x^4 - 2x^3 + 2x^2 + x - 1))) \\ &=& {\bf I}({\bf V}(x^4 - 2x^3 + 2x - 1)) \end{eqnarray*} $

ここで $f = x^4 - 2x^3 + 2x - 1$ とすると、Exercise 15.b より

$\begin{eqnarray*} f_{\text{red}} &=& \frac{f}{\text{GCD}(f, f')} \\ &=& \frac{x^4 - 2x^3 + 2x - 1}{\text{GCD}(x^4 - 2x^3 + 2x - 1, 4x^3 - 6x^2 + 2)} \\ &=& \frac{x^4 - 2x^3 + 2x - 1}{x^2 - 2x + 1} \\ &=& x^2 - 1 \end{eqnarray*}$

Exercise 12.b より

$\begin{eqnarray*} & & {\bf I}({\bf V}(x^5 - 2x^4 + 2x^2 - x, x^5 - x^4 - 2x^3 + 2x^2 + x - 1)) \\ &=& {\bf I}({\bf V}(f)) \\ &=& \langle f_{\text{red}}\rangle \\ &=& \langle x^2 - 1 \rangle \end{eqnarray*} $

6.01.2014

Python: How to Send an E-mail

Python: Eメールの送信

 

何度も車輪の再発明を繰り返しているが、今のところちょっとしたスクリプトにおいては
以下の内容で落ち着いている。

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

import os
import ConfigParser
import codecs
import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate

# load config
CONF_PATH = os.path.join(os.path.dirname(__file__), 'mail_sender.conf')

config = ConfigParser.SafeConfigParser()
config.readfp(codecs.open(CONF_PATH, 'r', 'utf-8'))

SMTP_SERVER = config.get('system', 'smtp-server')
SMTP_PORT = config.getint('system', 'smtp-port')


class MailSender(object):
    """General mail sending class"""

    def __init__(self, project_id):
        self.from_addr = config.get(project_id, 'from-address')
        self.to_addr_list = [a.strip() for a in config.get(project_id, 'to-address-list').split(',')]

    def send(self, subject, body, from_addr=None, to_addr_list=None):
        """send a message using SMTP"""

        if from_addr is None:
            from_addr = self.from_addr
        if to_addr_list is None:
            to_addr_list = self.to_addr_list
        msg = MailSender.__create_message(from_addr, to_addr_list, subject, body)

        s = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        s.sendmail(from_addr, to_addr_list, msg.as_string())
        s.close()

    @staticmethod
    def __create_message(from_addr, to_addr_list, subject, body, encoding='iso-2022-jp'):
        """create MIMEText object"""

        msg = MIMEText(body.encode(encoding), 'plain', encoding)
        msg['Subject'] = Header(subject, charset=encoding, header_name='Subject')
        msg['From'] = from_addr
        msg['To'] = ','.join(to_addr_list)
        msg['Date'] = formatdate(localtime=True)
        return msg


if __name__ == '__main__':
    # test code
    #MailSender('awesome-project').send(u'TITLE', u'THIS IS A TEST MAIL!')
    pass

同じ階層に conf ファイルを置く。

[system]
smtp-server: {{ smtp_server }}
smtp-port: {{ smtp_port }}

[awesome-project]
from-address: {{ from_address }}
to-address-list: {{ to_address_list }}

{{ }} の部分は適宜書き換え。to_address_list はカンマ区切りで複数指定可能。

Python: How to Log Message to SysLog

Python: SysLog にメッセージを書き込む方法

 

syslog パッケージを使う。

 

使用例

スクリプトファイル中での使用を想定。

import sys
import os
import syslog

def log_syslog(priority, message):
    prefix = {
        syslog.LOG_ERR: 'ERROR',
        syslog.LOG_WARNING: 'WARN',
        syslog.LOG_INFO: 'INFO',
    }[priority]
    syslog.openlog(os.path.basename(sys.argv[0]))
    syslog.syslog(priority, '%-7s%s' % ('[%s]' % prefix, message))

def log_error(message):
    log_syslog(syslog.LOG_ERR, message)

def log_warn(message):
    log_syslog(syslog.LOG_WARNING, message)

def log_info(message):
    log_syslog(syslog.LOG_INFO, message)

log_error("error message")
log_warn("warn message")
log_info("info message")

SysLog の内容

May 31 15:11:34 xxxx syslog_test.py: [ERROR]error message
May 31 15:11:34 xxxx syslog_test.py: [WARN] warn message
May 31 15:11:34 xxxx syslog_test.py: [INFO] info message

5.31.2014

Docker: Python: Getting Internal IP Address of the Container

コンテナを Detached Mode で起動して、その内部アドレスを知る一行野郎 (ワンライナー)

grep や jq でもできるけど、Python で JSON をパーズするほうが好み。

docker inspect $(docker run -d IMAGE_NAME) | \
python -c 'import json,sys;print json.loads(sys.stdin.read())[0]["NetworkSettings"]["IPAddress"]'

 

実行例

Docker の動作するホスト上で実行

# docker inspect $(docker run -d mogproject/sshd) | python -c 'import json,sys;print json.loads(sys.stdin.read())[0]["NetworkSettings"]["IPAddress"]'
Unable to find image 'mogproject/sshd' locally
Pulling repository mogproject/sshd
db38b1b40c65: Download complete
539c0211cd76: Download complete
154a1496c839: Download complete
0e8300b2772a: Download complete
43af4236d0da: Download complete
fc0cc09b2562: Download complete
172.17.0.2
# ssh ssh-user@172.17.0.2
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
RSA key fingerprint is e7:77:e5:bc:b0:3e:c4:78:27:cf:57:3a:2e:98:69:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.2' (RSA) to the list of known hosts.
ssh-user@172.17.0.2's password:
[ssh-user@1e53a210ba41 ~]$ cat /etc/redhat-release
CentOS release 6.4 (Final)

5.27.2014

Docker: Pull From the Private Registry Failed with I/O Timeout

Docker: プライベートレジストリからの docker pull でタイムアウトエラー

 

Mac + boot2docker 環境で、プライベートリポジトリから docker pull した時に
タイムアウトになってしまう問題。

$  docker -H tcp://127.0.0.1:4243 pull my-private-reg:5000/image_name:base
2014/05/27 00:59:47 Error: Invalid Registry endpoint: Get http://my-private-reg:5000/v1/_ping: dial tcp: i/o timeout

どうやら、名前解決で時間がかかっているようだった。(5秒以上)

以下いずれかの対応でとりあえずタイムアウトは回避できた。

  • 1. boot2docker-vm 上 (Mac上ではない!) の /etc/hosts にホストを登録し、ローカルで名前解決
  • 2. docker pull の際にプライベートリポジトリの IPアドレスを直接指定

 

追記:
よく見たら boot2docker-vm 上の /etc/resolv.conf 先頭に到達不能な IPアドレスが記述されていた。
boot2docker 起動時に Mac の設定情報をコピーするようなので、IPアドレスの変わりやすい ノートPC 利用時などでは特に注意が必要。

5.26.2014

IVA Reading: Chapter 01, Section 04 Exercises

IVA読書会 chap01-sect04 宿題

 

No.5

${\bf V}(x+xy,y+xy,x^2,y^2) = {\bf V}(x, y)$ を示す

 

Exercise 3 b. から、$\langle x+xy,y+xy,x^2,y^2 \rangle = \langle x,y \rangle$ が示されたので
命題4から最初の式が求まる。

また、明らかに ${\bf V}(x+xy,y+xy,x^2,y^2) = {\bf V}(x, y) = \{(0,0)\}$。

 

No.11

$(t, t^3, t^4)$ とパラメータ化された曲線 $V \subset \mathbb{R}^3$ を考える。

a. $V$ がアフィン多様体であることを証明する

$ \begin{cases} x = t \\ y = t^3 \\ z = t^4 \end{cases} $
より、
$V = {\bf V}(x-t,y-t^3,z-t^4) = {\bf V}(y - x^3, z - x^4) \subset \mathbb{R}^3$ が示される。

 

b. このねじれ3次曲線から ${\bf I}(V)$ を特定する

$V = {\bf V}(y - x^3, z - x^4) \subset \mathbb{R}^3$ について考えれば、 section04 本文中の証明と同様に
${\bf I}(V) = \langle y - x^3, z - x^4 \rangle$ を示すことができる。

 

途中式

$$ \begin{eqnarray*} x^{\alpha}y^{\beta}z^{\gamma} &=& x^{\alpha}(x^3+(y-x^3))^{\beta}(x^4+(z-x^4))^{\gamma} \\ &=& x^{\alpha}(x^{3\beta}+ \text{terms involving } y - x^3)(x^{4\gamma}+ \text{terms involving } z -x^4)) \\ &=& h_1(y - x^3) + h_2(z - x^4) + x^{\alpha + 3{\beta} + 4{\gamma}} \end{eqnarray*} $$

5.24.2014

How to Install MathJax on Blogger

BloggerでMathJaxを使う方法

 

Blogger テンプレートの編集

こちらの解説 (Getting Started — MathJax 2.3 documentation) にあるように、HTMLソースのHEADブロックの中に以下のタグを埋め込めばいい。

<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type="text/x-mathjax-config">  
  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>

Blogger の管理画面から、テンプレート -> HTML の編集 を行い、<head> … </head> の間の適当な場所に
タグを貼り付けて「テンプレートを保存」。

テンプレート -> バックアップ/復元 の機能を利用して、事前・事後にバックアップを取っておくと安心。

 

MarsEdit プレビューテンプレートの編集

ブログ作成ツール MarsEdit のプレビュー画面でも数式を表示したい。

これには SyntaxHighlighter 同様のトリックが必要となる。

テンプレートのBODYブロック内(</body>の直前)に以下のスクリプトを埋め込んだら、プレビュー画面でも MathJax が処理されるようになった。

<script type="text/javascript">
  function refreshMathJax() {
    MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
    setTimeout("refreshMathJax()", 5000);
  }
  refreshMathJax();
</script> 

 

出力サンプル

When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$

When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$