Mathematica 输入与输出

 PUBLISHED ON February 07, 2015

基本输出

Mathematica的基本输出命令是Print,具体用法为:

  • Print[expr]

    输出 expr. 

例如:

Print["Hello world"]
Hello world
Print[2^10]
1024

Print命令也可以一次输出多项内容(用逗号,隔开),输出时每项内容之间无间隔:

Print["Hello world", 2^10]
Hello world1024


Mathematica 笔记本(notebook)

 PUBLISHED ON February 05, 2015

本文主要记录Mathematica笔记本(notebook)使用过程中的一些小细节(Tips)。

注释

所有包含在(**)之间的内容为注释内容,都会被Mathematica内核忽略。

Mathematica区分字母大小写

所有的Mathematica命令都是以大写字母开头的,Mathematica区分字母大小写。因此,为了避免冲突,所有的用户定义符号都应当用小写字母开头。

函数参数

Mathematica中用方括号[]表示函数参数。例如:


cx_Freeze打包Python应用程序

 PUBLISHED ON February 05, 2015

cx_Freeze是一个打包Python应用的工具,通过cx_Freeze,可以制作Windows平台、MacOS平台、以及Linux平台的二进制可执行程序,使得Python应用能够在没有安装Python环境的机器上运行。

cx_Freeze的安装

同很多Python的第三方库类似,cx_Freeze也可以通过如下命令安装:

pip install cx_Freeze

或者从官网下载安装包,解压后,切换目录至解压后的根目录,运行命令

python setup.py install

即可。


Python编译成Pyc和Pyo文件

 PUBLISHED ON February 03, 2015

什么是pyc文件和pyo文件

pyc文件是Python的字节码(byte code)文件,是一种二进制文件。pyc文件跨平台,由python的虚拟机加载执行。pyc文件与Python的版本有关,不同版本的Python编译出的pyc文件不同。

pyo文件是优化(optimize)后的字节码文件。

编译成pyc文件

可以在命令行执行以下命令来将Python源码文件编译成pyc文件:

python -m py_compile $filename

其中,$filename是要编译的Python源码文件名。

也可以编写一下脚本来将Python源码文件编译成pyc文件:


C和C++中的sleep函数

 PUBLISHED ON February 03, 2015

sleep这一功能在不同的编译器中实现不同,API也有差别。

GCC编译器的实现

GCC编译器中,sleep定义在<unistd.h>中。函数原型:

/* The sleep() function is, perhaps, the most commonly used of all the
 * process/thread suspension APIs; it provides support for specification
 * of suspension periods ranging from 1 second to ~136 years.  (However,
 * POSIX recommends limiting the maximum period to 65535 seconds, to
 * maintain portability to platforms with only 16-bit ints).
 */
unsigned _cdecl __MINGW_NOTHROW sleep( unsigned );

注意,在GCC的实现中,sleep的单位为秒(second)。


Grep

 PUBLISHED ON February 02, 2015

指定要搜索的单词

grep -w "word"

搜索以特定字符串开头的单词

grep '\<word'

搜索以特定字符串结尾的单词

grep 'word\>'

搜索以特定字符串开头的行


Dive Into Haskell(1) GHCi

 PUBLISHED ON February 02, 2015

What's GHC ?

GHC(Glasgow Haskell Compiler): an interactive and batch compilation system for the Haskell 98 language.

GHC has two main components: an interactive Haskell interpreter (also known as GHCi), and a batch compiler, described throughout. In fact, GHC consists of a single program which is just run with different options to provide either the interactive or the batch system.

What's GHCi ?

GHCi是一个GHC的交互式环境,在GHCi中可以交互式地求得对Haskell表达式的值,Haskell程序也可以在GHCi中被解释。GCHi还包含了一个交互式的Haskell调试器。

GHCi的常用命令:


C语言中的函数指针

 PUBLISHED ON February 02, 2015

函数指针是C语言的灵魂之一,基于C语言实现的面向对象,底层都以函数指针为基础。

函数指针的定义

C语言中,可以通过以下方式定义函数指针:

type (*fn)(args)

其中,fn为指针变量名,type为指针所指向的函数的返回值类型,args为指针所指向的函数的参数列表。可以通过&运算符对函数指针赋值,通过(*fn)()的方式调用。举例:


C和C++中的回调函数

 PUBLISHED ON February 02, 2015

回调函数(Callback Functions)

回调函数是指一个通过函数指针调用的函数。回掉函数不是由该函数的实现方法直接调用,而是在特定时间或条件发生时由另一方调用的,用于对该事件或条件进行响应。

回调函数的简单实现

C和C++中,可以通过函数指针的方式实现回调函数。如下例:


Python Decorator

 PUBLISHED ON February 01, 2015

Decorator, 装饰器。Decorator的用途在于允许在函数和类中嵌入或者修改代码。

classmethod and staticmethod

staticmethod(function)

这将返回一个function的静态方法。把类的一个方法的声明为静态方法,具体用法如下:

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

或者

class C:
    def f(arg1, arg2, ...): ...
    f = staticmethod(f)