计算可塑性指数RDPI

可塑性指数RDPI的计算公式如下:

需要补充一点,i不能等于i‘。

假设a为集合A的元素,b为集合B的元素,则对于a和b在可塑性指数中的贡献为tmp=(Math.abs(parseFloat(a[i])-parseFloat(b[j])))/(parseFloat(a[i])+parseFloat(b[j])),随后求和取平均值,为了通用性和界面友好,采用HTML+Javascript构建本脚本。版本一(calRDPI v1)如下

<html>
<body>

<h1>计算RDPI</h1>
	<form id="ctrl">
		<label>数组1,英文空格分割<input type="text" id="a_str" size="100"/></label><br />
		<label>数组2,英文空格分割<input type="text" id="b_str" size="100"/></label><br />
		<input type="button" id="submit" value="提交" />
		<input type="reset" id="reset" value="重置" /><br />
		<label>提示消息<input type="text" id="msg" readonly="ture" size="100" /></label>
	</form>

<script>

var oSubmit = document.getElementById("submit");
var oMsg = document.getElementById("msg");
oSubmit.onclick = init;

function init(){
	var oA = document.getElementById("a_str");
	var oB = document.getElementById("b_str");
	
	var a = oA.value.split(" ");
	var b = oB.value.split(" ");


	// var a = [1,2,3,4,5,6,8];
	// var b = [2,3,4,5,6,7,9];
	var sum_array = new Array();
	var sum_num = 0;
	for (var i = 0; i < a.length; i++){
		for (var j = 0; j < b.length; j++){
			if(a[i] != "" && b[j] != ""){
				var tmp = (Math.abs(parseFloat(a[i])-parseFloat(b[j])))/(parseFloat(a[i])+parseFloat(b[j]));
				sum_array.push (tmp);
				sum_num += tmp;
				console.log(tmp);
			}
		}
	}
	oMsg.value = "计算"+sum_array.length+"次,加和为"+sum_num;
}
</script>

</body>
</html>

 

当处理因素为4时,则有四组数组,手动遍历组合比较麻烦,可借助函数的调用,版本二(calRDPI v2)如下

<!DOCTYPE html>
<html>
<body>

<h1>计算RDPI</h1>
    <form id="ctrl">
        <label>数组1,英文空格或逗号分割<input type="text" id="a_str" size="100"/></label><br />
        <label>数组2,英文空格或逗号分割<input type="text" id="b_str" size="100"/></label><br />
        <label>数组3,英文空格或逗号分割<input type="text" id="c_str" size="100"/></label><br />
        <label>数组4,英文空格或逗号分割<input type="text" id="d_str" size="100"/></label><br />

        <input type="button" id="submit" value="提交" />
        <input type="reset" id="reset" value="重置" /><br />
        <label>提示消息<input type="text" id="msg" readonly="ture" size="100" /></label>
    </form>

<script>

var oSubmit = document.getElementById("submit");
var oMsg = document.getElementById("msg");
function calOnce(s1,s2){
    var part_array = new Array();
    var part_num = 0;
    var a = s1.split(",");
    var b = s2.split(",")
    for (var i = 0; i < a.length; i++){
        for (var j = 0; j < b.length; j++){
            if(a[i] != "" && b[j] != ""){
                var tmp = (Math.abs(parseFloat(a[i])-parseFloat(b[j])))/(parseFloat(a[i])+parseFloat(b[j]));
                part_array.push (tmp);
                part_num += tmp;
            }
        }
    }
    return [part_num,part_array];
}

oSubmit.onclick = init;


function init(){
    var oA = document.getElementById("a_str");
    var oB = document.getElementById("b_str");
    var oC = document.getElementById("c_str");
    var oD = document.getElementById("d_str");
    
    var reg = /\s+|,/;
    var a = oA.value.split(reg);
    var b = oB.value.split(reg);
    var c = oC.value.split(reg);
    var d = oD.value.split(reg);
    var myData = new Array(a,b,c,d);

    var sum_array = new Array();
    var sum_length = 0;
    var sum_num = 0;

    for (var arr1 of myData){
        for (var arr2 of myData){
            if (arr1 != arr2){
                var tmpArr = calOnce(arr1.toString(),arr2.toString());
                //console.log(tmpArr[1].length)
                sum_array.push (tmpArr[1]);
                sum_length += tmpArr[1].length;
                sum_num += tmpArr[0];
            }
        }
    }
    oMsg.value = "计算"+sum_length+"次,加和为"+sum_num+"平均数是"+sum_num/sum_length;
    console.log(sum_array)
}
</script>

</body>
</html>

当有更多处理或者数据时,则需要以多行的形式一次性输入,需要扩展性更强的最终版本calRDPI v3

<!DOCTYPE html>
<html>
<body>

<h1>计算RDPI</h1>
    <form id="ctrl">
        <textarea rows="15" cols="100" id="str">英文空格或逗号为分隔符,换行分割数组,示例:


1,2,3,4,5,7
1 2 3 5 6
12,13,14,15
199,500</textarea><br/>
        <input type="button" id="submit" value="提交" />
        <input type="reset" id="reset" value="重置" /><br />
        <label>提示消息<input type="text" id="msg" readonly="ture" size="100" /></label>
    </form>

<script>

var oSubmit = document.getElementById("submit");
var oMsg = document.getElementById("msg");
function calOnce(s1,s2){
    var part_array = new Array();
    var part_num = 0;
    var reg = /\s+|,/;
    var a = s1.split(reg);
    var b = s2.split(reg)
    for (var i = 0; i < a.length; i++){
        for (var j = 0; j < b.length; j++){
            if(a[i] != "" && b[j] != ""){
                var tmp = (Math.abs(parseFloat(a[i])-parseFloat(b[j])))/(parseFloat(a[i])+parseFloat(b[j]));
                part_array.push (tmp);
                part_num += tmp;
            }
        }
    }
    return [part_num,part_array];
}

oSubmit.onclick = init;

function init(){
    var oStr = document.getElementById("str");
    var myData = oStr.value.split("\n");

    var sum_array = new Array();
    var sum_length = 0;
    var sum_num = 0;

    for (var arr1 of myData){
        for (var arr2 of myData){
            if (arr1 != arr2){
                var tmpArr = calOnce(arr1,arr2);
                sum_array.push (tmpArr[1]);
                sum_length += tmpArr[1].length;
                sum_num += tmpArr[0];
            }
        }
    }
    oMsg.value = "计算"+sum_length+"次,加和为"+sum_num+"平均数是"+sum_num/sum_length;
    console.log(sum_array)
}
</script>

</body>
</html>

在编写脚本时,发现JavaScript的Array的split(" ")方法,不仅可以识别空格,也可以识别英文逗号,但为了减少不确定性,后两个版本使用了正则/\s+|,/作为定界符。

为解决浏览器编码问题,添加HTML meta信息<meta charset="utf-8">,版本v3.1如下

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta name="description" content="相对距离可塑性指数(RDPI)计算" />
<meta name="author" content="Lyle Liu; liulele62@163.com" />
<title>相对距离可塑性指数(RDPI)计算</title>
<body>

<h1>相对距离可塑性指数(RDPI)计算V3.1</h1>
    <form id="ctrl">
        <textarea rows="15" cols="100" id="str"></textarea><br/>
        <input type="button" id="submit" value="提交" />
        <input type="reset" id="reset" value="重置" /><br />
        <label>提示消息<input type="text" id="msg" readonly="ture" size="100" /></label>
    </form>
<div>
<p>说明</p>
英文空格或逗号为分隔符,换行分割数组,示例:<br/>
1,2,3,4,5,7<br/>
1 2 3 5 6<br/>
12,13,14,15<br/>
199,500<br/>
<br/>
<br/>
<p>作者:Lyle Liu<p/>
<p>邮箱:liulele622@163.com<p/>

<div>
<script>

var oSubmit = document.getElementById("submit");
var oMsg = document.getElementById("msg");
function calOnce(s1,s2){
    var part_array = new Array();
    var part_num = 0;
    var reg = /\s+|,/;
    var a = s1.split(reg);
    var b = s2.split(reg)
    for (var i = 0; i < a.length; i++){
        for (var j = 0; j < b.length; j++){
            if(a[i] != "" && b[j] != ""){
                var tmp = (Math.abs(parseFloat(a[i])-parseFloat(b[j])))/(parseFloat(a[i])+parseFloat(b[j]));
                part_array.push (tmp);
                part_num += tmp;
            }
        }
    }
    return [part_num,part_array];
}

oSubmit.onclick = init;

function init(){
    var oStr = document.getElementById("str");
    var myData = oStr.value.split("\n");

    var sum_array = new Array();
    var sum_length = 0;
    var sum_num = 0;

    for (var arr1 of myData){
        for (var arr2 of myData){
            if (arr1 != arr2){
                var tmpArr = calOnce(arr1,arr2);
                sum_array.push (tmpArr[1]);
                sum_length += tmpArr[1].length;
                sum_num += tmpArr[0];
            }
        }
    }
    oMsg.value = "计算"+sum_length+"次,加和为"+sum_num+",平均数是"+sum_num/sum_length;
    console.log(sum_array)
}
</script>

</body>
</html>

 v3.2增加了行数显示,并对负值进行了折中处理。

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta name="description" content="相对距离可塑性指数(RDPI)计算" />
<meta name="author" content="Lyle Liu; liulele62@163.com" />
<title>相对距离可塑性指数(RDPI)计算</title>
<body>

<h1>相对距离可塑性指数(RDPI)计算V3.2</h1>
    <form id="ctrl">
        <textarea rows="15" cols="100" id="str"></textarea><br/>
        <input type="button" id="submit" value="提交" />
        <input type="reset" id="reset" value="重置" /><br />
        <label>提示消息<input type="text" id="msg" readonly="ture" size="100" /></label>
    </form>
<div>
<p>说明</p>
英文空格或逗号为分隔符,换行分割数组,示例:<br/>
1,2,3,4,5,7<br/>
1 2 3 5 6<br/>
12,13,14,15<br/>
199,500<br/>
<br/>
<br/>
<p>作者:Lyle Liu<p/>
<p>邮箱:liulele622@163.com<p/>

<div>
<script>

var oSubmit = document.getElementById("submit");
var oMsg = document.getElementById("msg");
function calOnce(s1,s2){
    var part_array = new Array();
    var part_num = 0;
    var reg = /\s+|,/;
    var a = s1.split(reg);
    var b = s2.split(reg)
    for (var i = 0; i < a.length; i++){
        for (var j = 0; j < b.length; j++){
            if(a[i] != "" && b[j] != ""){
                var fA = parseFloat(a[i]);
                var fB = parseFloat(b[j]);
                if (fA * fB >= 0){
                    var tmp = (Math.abs(fA - fB))/(Math.abs(fA + fB));
                }else{
                    fA = 2 * fB - fA;
                    var tmp = (Math.abs(fA - fB))/(Math.abs(fA + fB));
                }
                part_array.push (tmp);
                part_num += tmp;
            }
        }
    }
    return [part_num,part_array];
}

oSubmit.onclick = init;

function init(){
    var oStr = document.getElementById("str");
    var myData = oStr.value.split("\n");

    var sum_array = new Array();
    var sum_length = 0;
    var sum_num = 0;

    for (var arr1 of myData){
        for (var arr2 of myData){
            if (arr1 != arr2){
                var tmpArr = calOnce(arr1,arr2);
                sum_array.push (tmpArr[1]);
                sum_length += tmpArr[1].length;
                sum_num += tmpArr[0];
            }
        }
    }
    oMsg.value = "共"+myData.length+"行,计算"+sum_length+"次,加和为"+sum_num+",平均数是"+sum_num/sum_length;
    //console.log(sum_array)
}
</script>

</body>
</html>

 

posted @ 2018-09-09 19:08  LeleLiu  阅读(1121)  评论(0编辑  收藏  举报