搜索此博客

2015年4月26日星期日

ubuntu下安装goldendict及离线词库

1.sudo apt-get install goldendict

2.播放语音
sudo apt-get install mplayer

sudo apt-get install ubuntu-restricted-addons

sudo apt-get install ubuntu-restricted-extras

3.批量解压离线词库

#! /bin/bash
# run this script to install goldendict and off-line dictionaries
# Only for Ubuntu
# created by longbin <beangr@163.com>
# 2014-03-28


function goldendict_essential_install(){
    for file in ${FILE_LIST}
    do
        trap 'echo -e "\nInterrupted by user"; exit' INT
        echo -e "\n======================================="
        echo -e "Preparing to install ${file} ..."
        echo -e "======================================="
        echo -e "\tsudo apt-get install ${file}"
        sudo apt-get install ${file}
    done

    RET_VAL=$(which goldendict)
    if [[ "${RET_VAL}" == "" ]] ;then
        echo "goldendict installed error."
        exit
    else
        echo "goldendict installed successfully."
    fi
}

function select_offline_dict_dir(){
    read -p "    Press <Enter> to install off-line dictionaries. "
    if [[ -d "${OFFLINE_DICTS_DIR}" ]] ;then
        return
    fi

    DIR_LIST=$(find . -maxdepth 3 -type d | sed '/^\.$/d' | sed 's#\./##')
    if [[ "${DIR_LIST}" == "" ]] ;then
        echo "Off-line dictionary directory not exists."
        exit
    fi

    PS3="Please select your goldendict off-line dictionary's dir: "
    select option in ${DIR_LIST}
    do
        if [[ -d "${option}" ]] ;then
            OFFLINE_DICTS_DIR=${option}
            break
        fi
    done
}

function goldendict_offline_dict_setup(){
    select_offline_dict_dir
    GOLDENDICT_OFFLINE_DICT_DIR=/usr/share/goldendict-wordnet/dic
    if ! [[ -d "${GOLDENDICT_OFFLINE_DICT_DIR}" ]] ;then
        sudo mkdir -p ${GOLDENDICT_OFFLINE_DICT_DIR}
    fi

    if [[ -d ${OFFLINE_DICTS_DIR} ]] ; then
        pushd ${OFFLINE_DICTS_DIR}
        ###########################
        TGZ_FILES=$(ls |grep tgz)
        if [[ "${TGZ_FILES}" == "" ]] ;then
            echo "None tgz file exists."
            exit
        fi
        for file in ${TGZ_FILES}
        do
            if [[ -d "${GOLDENDICT_OFFLINE_DICT_DIR}/${file%.tgz}" ]] ;then
                echo "directory ${GOLDENDICT_OFFLINE_DICT_DIR}/${file%.tgz} has already exist."
                sudo rm -rf ${GOLDENDICT_OFFLINE_DICT_DIR}/${file%.tgz}
            fi
            echo "tar -zxvf ${file} -C ${GOLDENDICT_OFFLINE_DICT_DIR}"
            sudo tar -zxvf ${file} -C ${GOLDENDICT_OFFLINE_DICT_DIR}
        done
        ##########################
        popd
    else
        echo "Off-line dictionary directory \"${OFFLINE_DICTS_DIR}\" not exists."
        echo 'Please download dictionaries to \"${OFFLINE_DICTS_DIR}\" '
    fi
}

function install_mplayer_mutiple_support(){
    for file in ${MULTIPLE_SUPPORT} ${MULTIPLE_SUPPORT}
    do
        echo -e "\n======================================="
        echo -e "Preparing to install ${file} ..."
        echo -e "======================================="
        echo -e "\tsudo apt-get install ${file} ..."
        sudo apt-get install ${file}
    done
}

echo "Preparing to install goldendict and off-line dictionaries"

goldendict_essential_install
goldendict_offline_dict_setup




4.配置
Preferences  Files  词库位置 /usr/share/goldendict-wordnet/dic
Preferences Audio  Playback  mplayer

5.在线词典

http://www.iciba.com/%GDWORD%/

去在线词典广告,本地新建 dict.html

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <style>
        iframe{ width: 706px; height: 650px; margin-top:-190px; margin-left:-120px;
        }
    </style>
</head>

<body>
    <iframe id="a" frameborder="0">
    </iframe>
    <script>
    var word = location.href.slice(location.href.indexOf('?a')+3);
    document.getElementById('a').setAttribute(
        'src',
        'http://dict.youdao.com/search?le=eng&q=' + word + '&keyfrom=dict.top');
    </script>
</body>

网站  加入本地地址file:///home/xuefliang/Documents/goldendict_for_ubuntu/dict.html?a=%GDWORD%

R 3.1 -> 3.2 upgrade notes

1.cp -r ~/R/x86_64-pc-linux-gnu-library/3.1/* ~/R/x86_64-pc-linux-gnu-library/3.2 # at the shell prompt
2.update.packages(checkBuilt=TRUE, ask=FALSE) #at the R prompt

R语言的导数计算

求导通用法则
(u/v)'=(u'v-uv')/v^2

(uv)'=u'v+uv'

(cu)'=cu'

(u+v)'=u'+v'

dy/dt=dy/dx*dx/dt

D_{n}x_{n}=n!

一元初等函数的导数计算公式 

函数          原函数            导函数
常数函数      y=C               y'=0
幂函数        y=x^n             y'=n*x^(n-1)
指数函数      y=a^x             y'=a^x*ln(a)
              y=exp(1)^x        y'=exp(1)^x
对数函数      y=log(x,base=a)   y'=1/(x*ln(a)) (a>0,且a!=1,x>0)
              y=ln(x)           y'=1/x
正弦函数      y=sin(x)          y'=cos(x)
余弦函数      y=cos(x)          y'=-sin(x)
正切函数      y=tan(x)          y'=sec(x)^2=1/cos(x)^2
余切函数      y=cot(x)          y'=-csc(x)^2=1/sin(x)^2
正割函数      y=sec(x)          y'=sec(x)*tan(x)
余割函数      y=csc(x)          y'=-csc(x)*cot(x)
反正弦函数    y=arcsin(x)       y'=1/sqrt(1-x^2)
反余弦函数    y=arccos(x)       y'=-1/sqrt(1-x^2)
反正切函数    y=arctan(x)       y'=1/(1+x^2)
反余切函数    y=arccot(x)       y'=-1/(1+x^2)
反正割函数    y=arcsec(x)       y'=1/abs(x)*(x^2-1)
反余割函数    y=arccsc(x)       y'=-1/abs(x)*(x^2-1)



1、stats包
dx <- deriv(y~x^3,"x")
x<-1:2  #给自变量x赋值
eval(dx) #运行求导计算

deriv(expr, name)函数时通常要传2个参数,第一参数expr就是原函数公式,用~号来分隔公式的两边,第二参数name用于指定函数的自变量。deriv()函数会返回一个表达式expression类型变量,再用eval()函数运行这个表达式得到就可得到计算结果。希望以函数的形式调用计算公式,那么你还需要传第三个参数func,并让func参数为TRUE

dx <- deriv(y~(sin(x))^10,"x",func=T)  # 生成导数公式的调用函数
dx(4)   # 以参数作为自变量,进行函数调用

dx<-deriv3(y~A*x^2,"x",func = TRUE) #二阶导数

2、mosaic包
library(mosaic)
D(sin(x) ~ x)  #sinx求导,D()屏蔽了stats包中的D()
D(A * x^2~ x + x) #A*x的2次方的二阶导数
y <- D((sin(x))^10 ~ x) # (sinx)10次方求导
y(4)

单变量定积分

stats包
integrate(dnorm, -1.96, 1.96)
integrate(dnorm, -Inf, Inf)

## 自定义函数
integrand <- function(x) {1/((x+1)*sqrt(x))}
integrate(integrand, lower = 0, upper = Inf)



2015年4月14日星期二

formatR使用

1 建立R脚本文件
 demo.r
a<-1+1;a;matrix(rnorm(10),5);
if(a>2) { b=c('11',832);"#a>2";} else print('a is invalid!!')

x = "demo.r"

tidy_source(x)

2 GUI 工具
formatR::tidy_app()

2015年4月12日星期日

如何用R来处理图片

1 biOps安装
sudo apt-get install libfftw3-dev libtiff5-dev
devtools::install_github("cran/biOps")

2 使用
library(biOps)
# 读取本地的图像文件,观察到此图像是320*320像素的rgb图片
x <-  readJpeg("d:\\xccds.jpg")
print(x)
# 先尝试缩放操作,后面的参数是缩放的比例,可以采用四种方式进行插值,这里用的是最近邻法
plot(imgScale(x,1.3,1.3,interpolation='nearestneighbor'))
#之后进行柔化降噪,此处采用的是中位数滤镜
plot(imgBlockMedianFilter(x,5))

# 也很方便进行图片裁剪,先设定好左上角的坐标像素,再设定好图片的宽度和高度即可
plot(imgCrop(x,1,1,320,200))
# 修改后的结果可以用writeJpeg函数存到本地磁盘中
y <- imgCrop(x,1,1,320,200)
writeJpeg(filename='test.jpg',imgdata=y)

# 另外还有一些有用的处理函数
# 图像聚类以进行颜色的压缩
plot(imgKMeans(x,8))

# 翻转图像
plot(imgHorizontalMirroring(x))
# 负片效果
plot(imgNegative(x))
# 转灰度效果
plot(imgRGB2Grey(x))

#图像背景色更换
y <- unclass(x)
y1 <- y[,,1]
y2 <- y[,,2]
y3 <- y[,,3]
#白色255 255 255
z <- y1>220&y2>220&y3>220
y1[z] <- 255  #r
y2[z] <-0       #g
y3[z]<- 0       #b
# 修改后的矩阵再重新组合为图片格式
y <- imagedata(array(data=c(y1,y2,y3),dim=c(320,320,3)))
plot(y)

R软件矩阵使用总结

1 矩阵转置
A为m×n矩阵,求a'在R中可用函数t()
>A <- matrix(c(2,1,1,2),nrow = 2)
t>(A)
2 矩阵相加减
R中对同行同列矩阵相加减,可用符号:“+”、“-”,
3 数与矩阵相乘
A为m×n矩阵,c>0,在R中求cA可用符号:“*”
4  矩阵相乘
A为m×n矩阵,B为n×k矩阵,在R中求AB可用符号:“%*%”
> A=matrix(1:12,nrow=3,ncol=4)
> B=matrix(1:12,nrow=4,ncol=3)
> A%*%B
5 矩阵秩
>library(Matrix)
>rankMatrix(A)
6 矩阵的特征值与特征向量
>eigen(A)  #A为方阵
7 矩阵求逆
矩阵求逆可用函数solve(),应用solve(a, b)运算结果是解线性方程组ax = b,若b缺省,则系统默认为单位矩阵,因此可用其进行矩阵求逆
> solve(a)
8 矩阵对角元素相关运算
取一个方阵的对角元素
>diag(A)
函数将产生以这个向量为对角元素的对角矩阵
>diag(diag(A))
对一个正整数z应用diag()函数将产生以z维单位矩阵
>diag(3)
9 行列式的值
det(a)
 行列式的值=方阵的特征值的乘积
A的迹(矩阵对角元素的和)= 方阵的特征值的和
prod<- function(b){
  prod<- 1
  for(i in 1:length(b)){
    prod<- prod*b[i]
  }
  prod
}
prod(b$value)=det(a)
sum(b$value)=sum(diag(a))
10 对角阵
diag(eigen(a)$values)

例题  R解多元一次方程,方程如下
x+2y+2=7
2x-y+3z=7
3x+y+2z=18
R中求解如下
a <- matrix(c(1,2,3,2,-1,1,1,3,2),nrow = 3)
b <- c(7,7,18)
solve(a,b)

2015年4月10日星期五

Install R in Ubuntu

Step 1: Update Sources.List File
- Edit the sources.list file
sudo gedit /etc/apt/sources.list
- Add following entry
deb http://cran.rstudio.com/bin/linux/ubuntu utopic/
Step 2: Add the Public Keys
gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -
Step 3: Install R-base
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install r-base build-essential gfortran libxml2-dev libcurl4-openssl-dev libfreetype6-dev libbz2-dev liblapack-dev  libpcre++-dev  r-base-dev  liblzma-dev r-cran-rcpp r-cran-rjava openjdk-7-jre

wants <- c("knitr","rmarkdown","devtools","epicalc","mosaic","showtext","pander","PerformanceAnalytics","fitdistrplus","CircStats","MASS","mixtools","boot","TrialSize","vcd","ggplot2","pspearman","gvlma","car","lmtest","leaps","plyr","bootstrap","elrm","rms","Deducer","bestglm","survival","robust","mlogit", "nnet", "VGAM","ordinal","Sample.Size","samplesize4surveys","SampleSizeMeans","SampleSizeProportions","mosaic","XML","pipeR","formatR","shiny","expm","koRpus")
has   <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])

devtools::install_github("rstudio/rticles")
devtools::install_github("rstudio/rmarkdown")