java分数表示_表示Java分数的最佳方法?

小编典典

碰巧的是不久前我写了一个BigFraction类,用于解决Euler项目问题。它保留了BigInteger分子和分母,因此它将永远不会溢出。但是,对于许多你永远不会溢出的操作来说,这会有点慢。无论如何,请根据需要使用它。我一直很想以某种方式炫耀它。:)

编辑:该代码的最新,最出色的版本,包括单元测试,现在托管在GitHub上,也可以通过Maven Central获得。我将原始代码留在这里,以便此答案不仅仅是一个链接…

import java.math.*;

/**

* Arbitrary-precision fractions, utilizing BigIntegers for numerator and

* denominator. Fraction is always kept in lowest terms. Fraction is

* immutable, and guaranteed not to have a null numerator or denominator.

* Denominator will always be positive (so sign is carried by numerator,

* and a zero-denominator is impossible).

*/

public final class BigFraction extends Number implements Comparable

{

private static final long serialVersionUID = 1L; //because Number is Serializable

private final BigInteger numerator;

private final BigInteger denominator;

public final static BigFraction ZERO = new BigFraction(BigInteger.ZERO, BigInteger.ONE, true);

public final static BigFraction ONE = new BigFraction(BigInteger.ONE, BigInteger.ONE, true);

/**

* Constructs a BigFraction with given numerator and denominator. Fraction

* will be reduced to lowest terms. If fraction is negative, negative sign will

* be carried on numerator, regardless of how the values were passed in.

*/

public BigFraction(BigInteger numerator, BigInteger denominator)

{

if(numerator == null)

throw new IllegalArgumentException("Numerator is null");

if(denominator == null)

throw new IllegalArgumentException("Denominator is null");

if(denominator.equals(BigInteger.ZERO))

throw new ArithmeticException("Divide by zero.");

//only numerator should be negative.

if(denominator.signum() < 0)

{

numerator = numerator.negate();

denominator = denominator.negate();

}

//create a reduced fraction

BigInteger gcd = numerator.gcd(denominator);

this.numerator = numerator.divide(gcd);

this.denominator = denominator.divide(gcd);

}

/**

* Constructs a BigFraction from a whole number.

*/

public BigFraction(BigInteger numerator)

{

this(numerator, BigInteger.ONE, true);

}

public BigFraction(long numerator, long denominator)

{

this(BigInteger.valueOf(numerator), BigInteger.valueOf(denominator));

}

public BigFraction(long numerator)

{

this(BigInteger.valueOf(numerator), BigInteger.ONE, true);

}

/**

* Constructs a BigFraction from a floating-point number.

*

* Warning: round-off error in IEEE floating point numbers can result

* in answers that are unexpected. For example,

* System.out.println(new BigFraction(1.1))

* will print:

* 2476979795053773/2251799813685248

*

* This is because 1.1 cannot be expressed exactly in binary form. The

* given fraction is exactly equal to the internal representation of

* the double-precision floating-point number. (Which, for 1.1, is:

* (-1)^0 * 2^0 * (1 + 0x199999999999aL / 0x10000000000000L).)

*

* NOTE: In many cases, BigFraction(Double.toString(d)) may give a result

* closer to what the user expects.

*/

public BigFraction(double d)

{

if(Double.isInfinite(d))

throw new IllegalArgumentException("double val is infinite");

if(Double.isNaN(d))

throw new IllegalArgumentException("double val is NaN");

//special case - math below won't work right for 0.0 or -0.0

if(d == 0)

{

numerator = BigInteger.ZERO;

denominator = BigInteger.ONE;

return;

}

final long bits = Double.doubleToLongBits(d);

final int sign = (int)(bits >> 63) & 0x1;

final int exponent = ((int)(bits >> 52) & 0x7ff) - 0x3ff;

final long mantissa = bits & 0xfffffffffffffL;

//number is (-1)^sign * 2^(exponent) * 1.mantissa

BigInteger tmpNumerator = BigInteger.valueOf(sign==0 ? 1 : -1);

BigInteger tmpDenominator = BigInteger.ONE;

//use shortcut: 2^x == 1 << x. if x is negative, shift the denominator

if(exponent >= 0)

tmpNumerator = tmpNumerator.multiply(BigInteger.ONE.shiftLeft(exponent));

else

tmpDenominator = tmpDenominator.multiply(BigInteger.ONE.shiftLeft(-exponent));

//1.mantissa == 1 + mantissa/2^52 == (2^52 + mantissa)/2^52

tmpDenominator = tmpDenominator.multiply(BigInteger.valueOf(0x10000000000000L));

tmpNumerator = tmpNumerator.multiply(BigInteger.valueOf(0x10000000000000L + mantissa));

BigInteger gcd = tmpNumerator.gcd(tmpDenominator);

numerator = tmpNumerator.divide(gcd);

denominator = tmpDenominator.divide(gcd);

}

/**

* Constructs a BigFraction from two floating-point numbers.

*

* Warning: round-off error in IEEE floating point numbers can result

* in answers that are unexpected. See BigFraction(double) for more

* information.

*

* NOTE: In many cases, BigFraction(Double.toString(numerator) + "/" + Double.toString(denominator))

* may give a result closer to what the user expects.

*/

public BigFraction(double numerator, double denominator)

{

if(denominator == 0)

throw new ArithmeticException("Divide by zero.");

BigFraction tmp = new BigFraction(numerator).divide(new BigFraction(denominator));

this.numerator = tmp.numerator;

this.denominator = tmp.denominator;

}

/**

* Constructs a new BigFraction from the given BigDecimal object.

*/

public BigFraction(BigDecimal d)

{

this(d.scale() < 0 ? d.unscaledValue().multiply(BigInteger.TEN.pow(-d.scale())) : d.unscaledValue(),

d.scale() < 0 ? BigInteger.ONE : BigInteger.TEN.pow(d.scale()));

}

public BigFraction(BigDecimal numerator, BigDecimal denominator)

{

if(denominator.equals(BigDecimal.ZERO))

throw new ArithmeticException("Divide by zero.");

BigFraction tmp = new BigFraction(numerator).divide(new BigFraction(denominator));

this.numerator = tmp.numerator;

this.denominator = tmp.denominator;

}

/**

* Constructs a BigFraction from a String. Expected format is numerator/denominator,

* but /denominator part is optional. Either numerator or denominator may be a floating-

* point decimal number, which in the same format as a parameter to the

* BigDecimal(String) constructor.

*

* @throws NumberFormatException if the string cannot be properly parsed.

*/

public BigFraction(String s)

{

int slashPos = s.indexOf('/');

if(slashPos < 0)

{

BigFraction res = new BigFraction(new BigDecimal(s));

this.numerator = res.numerator;

this.denominator = res.denominator;

}

else

{

BigDecimal num = new BigDecimal(s.substring(0, slashPos));

BigDecimal den = new BigDecimal(s.substring(slashPos+1, s.length()));

BigFraction res = new BigFraction(num, den);

this.numerator = res.numerator;

this.denominator = res.denominator;

}

}

/**

* Returns this + f.

*/

public BigFraction add(BigFraction f)

{

if(f == null)

throw new IllegalArgumentException("Null argument");

//n1/d1 + n2/d2 = (n1*d2 + d1*n2)/(d1*d2)

return new BigFraction(numerator.multiply(f.denominator).add(denominator.multiply(f.numerator)),

denominator.multiply(f.denominator));

}

/**

* Returns this + b.

*/

public BigFraction add(BigInteger b)

{

if(b == null)

throw new IllegalArgumentException("Null argument");

//n1/d1 + n2 = (n1 + d1*n2)/d1

return new BigFraction(numerator.add(denominator.multiply(b)),

denominator, true);

}

/**

* Returns this + n.

*/

public BigFraction add(long n)

{

return add(BigInteger.valueOf(n));

}

/**

* Returns this - f.

*/

public BigFraction subtract(BigFraction f)

{

if(f == null)

throw new IllegalArgumentException("Null argument");

return new BigFraction(numerator.multiply(f.denominator).subtract(denominator.multiply(f.numerator)),

denominator.multiply(f.denominator));

}

/**

* Returns this - b.

*/

public BigFraction subtract(BigInteger b)

{

if(b == null)

throw new IllegalArgumentException("Null argument");

return new BigFraction(numerator.subtract(denominator.multiply(b)),

denominator, true);

}

/**

* Returns this - n.

*/

public BigFraction subtract(long n)

{

return subtract(BigInteger.valueOf(n));

}

/**

* Returns this * f.

*/

public BigFraction multiply(BigFraction f)

{

if(f == null)

throw new IllegalArgumentException("Null argument");

return new BigFraction(numerator.multiply(f.numerator), denominator.multiply(f.denominator));

}

/**

* Returns this * b.

*/

public BigFraction multiply(BigInteger b)

{

if(b == null)

throw new IllegalArgumentException("Null argument");

return new BigFraction(numerator.multiply(b), denominator);

}

/**

* Returns this * n.

*/

public BigFraction multiply(long n)

{

return multiply(BigInteger.valueOf(n));

}

/**

* Returns this / f.

*/

public BigFraction divide(BigFraction f)

{

if(f == null)

throw new IllegalArgumentException("Null argument");

if(f.numerator.equals(BigInteger.ZERO))

throw new ArithmeticException("Divide by zero");

return new BigFraction(numerator.multiply(f.denominator), denominator.multiply(f.numerator));

}

/**

* Returns this / b.

*/

public BigFraction divide(BigInteger b)

{

if(b == null)

throw new IllegalArgumentException("Null argument");

if(b.equals(BigInteger.ZERO))

throw new ArithmeticException("Divide by zero");

return new BigFraction(numerator, denominator.multiply(b));

}

/**

* Returns this / n.

*/

public BigFraction divide(long n)

{

return divide(BigInteger.valueOf(n));

}

/**

* Returns this^exponent.

*/

public BigFraction pow(int exponent)

{

if(exponent == 0)

return BigFraction.ONE;

else if (exponent == 1)

return this;

else if (exponent < 0)

return new BigFraction(denominator.pow(-exponent), numerator.pow(-exponent), true);

else

return new BigFraction(numerator.pow(exponent), denominator.pow(exponent), true);

}

/**

* Returns 1/this.

*/

public BigFraction reciprocal()

{

if(this.numerator.equals(BigInteger.ZERO))

throw new ArithmeticException("Divide by zero");

return new BigFraction(denominator, numerator, true);

}

/**

* Returns the complement of this fraction, which is equal to 1 - this.

* Useful for probabilities/statistics.

*/

public BigFraction complement()

{

return new BigFraction(denominator.subtract(numerator), denominator, true);

}

/**

* Returns -this.

*/

public BigFraction negate()

{

return new BigFraction(numerator.negate(), denominator, true);

}

/**

* Returns -1, 0, or 1, representing the sign of this fraction.

*/

public int signum()

{

return numerator.signum();

}

/**

* Returns the absolute value of this.

*/

public BigFraction abs()

{

return (signum() < 0 ? negate() : this);

}

/**

* Returns a string representation of this, in the form

* numerator/denominator.

*/

public String toString()

{

return numerator.toString() + "/" + denominator.toString();

}

/**

* Returns if this object is equal to another object.

*/

public boolean equals(Object o)

{

if(!(o instanceof BigFraction))

return false;

BigFraction f = (BigFraction)o;

return numerator.equals(f.numerator) && denominator.equals(f.denominator);

}

/**

* Returns a hash code for this object.

*/

public int hashCode()

{

//using the method generated by Eclipse, but streamlined a bit..

return (31 + numerator.hashCode())*31 + denominator.hashCode();

}

/**

* Returns a negative, zero, or positive number, indicating if this object

* is less than, equal to, or greater than f, respectively.

*/

public int compareTo(BigFraction f)

{

if(f == null)

throw new IllegalArgumentException("Null argument");

//easy case: this and f have different signs

if(signum() != f.signum())

return signum() - f.signum();

//next easy case: this and f have the same denominator

if(denominator.equals(f.denominator))

return numerator.compareTo(f.numerator);

//not an easy case, so first make the denominators equal then compare the numerators

return numerator.multiply(f.denominator).compareTo(denominator.multiply(f.numerator));

}

/**

* Returns the smaller of this and f.

*/

public BigFraction min(BigFraction f)

{

if(f == null)

throw new IllegalArgumentException("Null argument");

return (this.compareTo(f) <= 0 ? this : f);

}

/**

* Returns the maximum of this and f.

*/

public BigFraction max(BigFraction f)

{

if(f == null)

throw new IllegalArgumentException("Null argument");

return (this.compareTo(f) >= 0 ? this : f);

}

/**

* Returns a positive BigFraction, greater than or equal to zero, and less than one.

*/

public static BigFraction random()

{

return new BigFraction(Math.random());

}

public final BigInteger getNumerator() { return numerator; }

public final BigInteger getDenominator() { return denominator; }

//implementation of Number class. may cause overflow.

public byte byteValue() { return (byte) Math.max(Byte.MIN_VALUE, Math.min(Byte.MAX_VALUE, longValue())); }

public short shortValue() { return (short)Math.max(Short.MIN_VALUE, Math.min(Short.MAX_VALUE, longValue())); }

public int intValue() { return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, longValue())); }

public long longValue() { return Math.round(doubleValue()); }

public float floatValue() { return (float)doubleValue(); }

public double doubleValue() { return toBigDecimal(18).doubleValue(); }

/**

* Returns a BigDecimal representation of this fraction. If possible, the

* returned value will be exactly equal to the fraction. If not, the BigDecimal

* will have a scale large enough to hold the same number of significant figures

* as both numerator and denominator, or the equivalent of a double-precision

* number, whichever is more.

*/

public BigDecimal toBigDecimal()

{

//Implementation note: A fraction can be represented exactly in base-10 iff its

//denominator is of the form 2^a * 5^b, where a and b are nonnegative integers.

//(In other words, if there are no prime factors of the denominator except for

//2 and 5, or if the denominator is 1). So to determine if this denominator is

//of this form, continually divide by 2 to get the number of 2's, and then

//continually divide by 5 to get the number of 5's. Afterward, if the denominator

//is 1 then there are no other prime factors.

//Note: number of 2's is given by the number of trailing 0 bits in the number

int twos = denominator.getLowestSetBit();

BigInteger tmpDen = denominator.shiftRight(twos); // x / 2^n === x >> n

final BigInteger FIVE = BigInteger.valueOf(5);

int fives = 0;

BigInteger[] divMod = null;

//while(tmpDen % 5 == 0) { fives++; tmpDen /= 5; }

while(BigInteger.ZERO.equals((divMod = tmpDen.divideAndRemainder(FIVE))[1]))

{

fives++;

tmpDen = divMod[0];

}

if(BigInteger.ONE.equals(tmpDen))

{

//This fraction will terminate in base 10, so it can be represented exactly as

//a BigDecimal. We would now like to make the fraction of the form

//unscaled / 10^scale. We know that 2^x * 5^x = 10^x, and our denominator is

//in the form 2^twos * 5^fives. So use max(twos, fives) as the scale, and

//multiply the numerator and deminator by the appropriate number of 2's or 5's

//such that the denominator is of the form 2^scale * 5^scale. (Of course, we

//only have to actually multiply the numerator, since all we need for the

//BigDecimal constructor is the scale.

BigInteger unscaled = numerator;

int scale = Math.max(twos, fives);

if(twos < fives)

unscaled = unscaled.shiftLeft(fives - twos); //x * 2^n === x << n

else if (fives < twos)

unscaled = unscaled.multiply(FIVE.pow(twos - fives));

return new BigDecimal(unscaled, scale);

}

//else: this number will repeat infinitely in base-10. So try to figure out

//a good number of significant digits. Start with the number of digits required

//to represent the numerator and denominator in base-10, which is given by

//bitLength / log[2](10). (bitLenth is the number of digits in base-2).

final double LG10 = 3.321928094887362; //Precomputed ln(10)/ln(2), a.k.a. log[2](10)

int precision = Math.max(numerator.bitLength(), denominator.bitLength());

precision = (int)Math.ceil(precision / LG10);

//If the precision is less than 18 digits, use 18 digits so that the number

//will be at least as accurate as a cast to a double. For example, with

//the fraction 1/3, precision will be 1, giving a result of 0.3. This is

//quite a bit different from what a user would expect.

if(precision < 18)

precision = 18;

return toBigDecimal(precision);

}

/**

* Returns a BigDecimal representation of this fraction, with a given precision.

* @param precision the number of significant figures to be used in the result.

*/

public BigDecimal toBigDecimal(int precision)

{

return new BigDecimal(numerator).divide(new BigDecimal(denominator), new MathContext(precision, RoundingMode.HALF_EVEN));

}

//--------------------------------------------------------------------------

// PRIVATE FUNCTIONS

//--------------------------------------------------------------------------

/**

* Private constructor, used when you can be certain that the fraction is already in

* lowest terms. No check is done to reduce numerator/denominator. A check is still

* done to maintain a positive denominator.

*

* @param throwaway unused variable, only here to signal to the compiler that this

* constructor should be used.

*/

private BigFraction(BigInteger numerator, BigInteger denominator, boolean throwaway)

{

if(denominator.signum() < 0)

{

this.numerator = numerator.negate();

this.denominator = denominator.negate();

}

else

{

this.numerator = numerator;

this.denominator = denominator;

}

}

}

2020-03-19

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541468.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《OpenStack云计算实战手册(第2版)》——1.7 添加用户

本节书摘来自异步社区《OpenStack云计算实战手册&#xff08;第2版&#xff09;》一书中的第1章&#xff0c;第1.7节,作者&#xff1a; 【英】Kevin Jackson , 【美】Cody Bunch 更多章节内容可以访问云栖社区“异步社区”公众号查看。 1.7 添加用户 在OpenStack身份认证服务中…

开源软件和自由软件_自由和开源软件的经济学

开源软件和自由软件零边际成本 (Zero Marginal Cost) At the core of the financial aspects of Free and Open Source is the zero negligible expense of merchandise in an environment that is digital. Right now, the rise of Free and Open Source speaks to an affirma…

java外部类_Java里什么叫内部类什么叫外部类

展开全部对普通类(没有内部类的类)来说&#xff0c;62616964757a686964616fe78988e69d8331333337396234内部类和外部类都与他无关&#xff1b;对有内部类的类来说&#xff0c;它们就是其内部类的外部类&#xff0c;外部类是个相对的说法&#xff0c;其实就是有内部类的类。所以…

《精通Matlab数字图像处理与识别》一6.2 傅立叶变换基础知识

本节书摘来自异步社区《精通Matlab数字图像处理与识别》一书中的第6章&#xff0c;第6.2节&#xff0c;作者 张铮 , 倪红霞 , 苑春苗 , 杨立红&#xff0c;更多章节内容可以访问云栖社区“异步社区”公众号查看 6.2 傅立叶变换基础知识 精通Matlab数字图像处理与识别要理解傅立…

多线程循环输出abcc++_C ++循环| 查找输出程序| 套装5

多线程循环输出abccProgram 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){int num 15673;int R1 0, R2 0;do {R1 num % 10;R2 R2 * 10 R1;num num / 10;} while (num > 0);cout << R2 << " ";return 0;}Ou…

java oql_深入理解java虚拟机(八):java内存分析工具-MAT和OQL

以下内容翻译自MAT帮助文档。一、Class HistogramClass Histogram shows the classes found in the snapshot, the number of objects for each class, the heap memory consumption of these objects, and the minimum retained size of the objects二、Dominator treeDomina…

《Python数据分析与挖掘实战》一1.2 从餐饮服务到数据挖掘

本节书摘来自华章出版社《Python数据分析与挖掘实战》一书中的第1章&#xff0c;第1.2节&#xff0c;作者 张良均 王路 谭立云 苏剑林&#xff0c;更多章节内容可以访问云栖社区“华章计算机”公众号查看 1.2 从餐饮服务到数据挖掘 企业经营最大的目的就是盈利&#xff0c;而餐…

obj[]与obj._Ruby中带有示例的Array.include?(obj)方法

obj[]与obj.Ruby Array.include&#xff1f;(obj)方法 (Ruby Array.include?(obj) Method) In the previous articles, we have seen how we can check whether two Array instances are identical or not with the help of <> operator, operator, and .eql? method?…

java javah_Java开发网 - 一个javah的问题

Posted by:jerry_xuPosted on:2006-03-13 15:39我在环境变量中已经设置了path为D:\Program Files\Java\jdk1.5.0_06&#xff0c;ClassPath设置为.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;class的路径为&#xff1a;D:\JNItest\bin\jni\Hello.class &#xff0c;但是…

《Python面向对象编程指南》——2.7 __del__()方法

本节书摘来自异步社区《Python面向对象编程指南》一书中的第2章&#xff0c;第2.7节&#xff0c;作者&#xff3b;美&#xff3d;Steven F. Lott&#xff0c; 张心韬 兰亮 译&#xff0c;更多章节内容可以访问云栖社区“异步社区”公众号查看。 2.7 __del__()方法 __del__()方…

NullReferenceException C#中的异常

什么是NullReferenceException&#xff1f; (What is NullReferenceException?) NullReferenceException is an exception and it throws when the code is trying to access a reference that is not referencing to any object. If a reference variable/object is not refe…

java map key 大写转小写_Spring JdbcTemplate 查询出的Map,是如何产生大小写忽略的Key的?(转)...

Java 是区分大小写的&#xff0c;普通的Map例如HashMap如果其中的key"ABC" value"XXX"那么map.get("Abc") 或 map.get("abc")是获取不到值得。但Spring中产生了一个忽略大小写的map使我产生了好奇例如 jdbcTemplate.queryForList(sql)…

《iOS 6核心开发手册(第4版)》——2.11节秘诀:构建星星滑块

本节书摘来自异步社区《iOS 6核心开发手册&#xff08;第4版&#xff09;》一书中的第2章&#xff0c;第2.11节秘诀&#xff1a;构建星星滑块&#xff0c;作者 【美】Erica Sadun&#xff0c;更多章节内容可以访问云栖社区“异步社区”公众号查看 2.11 秘诀&#xff1a;构建星星…

css框架和js框架_优雅设计的顶级CSS框架

css框架和js框架Brief discussion: 简要讨论&#xff1a; Well, who doesnt want their website or web page to look attractive, stylish and be responsive? 那么&#xff0c;谁不希望自己的网站或网页看起来有吸引力&#xff0c;时尚并且ReactSwift&#xff1f; We put …

软考下午题具体解释---数据流图设计

在历年的软考下午题其中&#xff0c;有五道大题。各自是数据流图的设计&#xff0c;数据库设计&#xff0c;uml图&#xff0c;算法和设计模式&#xff0c;从今天这篇博文開始&#xff0c;小编就跟大家来一起学习软考下午题的相关内容。包含理论上的知识以及典型例题的解说&…

基本程序 打印Scala的Hello World

Scala中的基本程序 (Basic program in Scala) As your first Scala program, we will see a basic output program that just prints "Hello World" or any other similar type of string. With this example, we will see what are the part of the code that is im…

java treemap lastkey_Java TreeMap lastKey()用法及代码示例

java.util.TreeMap.lastKey()用于检索Map中存在的最后一个或最高键。用法:tree_map.lastKey()参数&#xff1a;该方法不带任何参数。返回值&#xff1a;该方法返回映射中存在的最后一个键。异常&#xff1a;如果映射为空&#xff0c;则该方法将引发NoSuchElementException。以下…

mysql属于数据库三级模式_数据库系统的三级模式指的是什么

数据库系统的三级模式指的是什么发布时间&#xff1a;2020-10-26 10:11:21来源&#xff1a;亿速云阅读&#xff1a;52作者&#xff1a;小新小编给大家分享一下数据库系统的三级模式指的是什么&#xff0c;希望大家阅读完这篇文章后大所收获&#xff0c;下面让我们一起去探讨吧&…

《自顶向下网络设计(第3版)》——导读

目录 第1部分 辨明客户的需求和目标 第1章 分析商业目标和制约 1.1 采用自顶向下的网络设计方法 1.2 分析商业目标 1.3 分析商业制约 1.4 商业目标检查表 1.5 小结 1.6 复习题 1.7 设计环境 第2章 分析技术目标与折衷措施 2.1 可扩展性 2.2 可用性 2.3 网络性能 2.4 安全性 2…

python矩阵变化_用numpy改变矩阵的形状

我的问题有两个方面。我有下面的代码来处理一些矩阵。在import numpytupleList [(0, 122), (1, 246), (2, 157), (3, 166), (4, 315), (5, 108), (6, 172), (7, 20), (8, 173), (9, 38), (10, 28), (11, 72), (12, 102), (13, 277), (14, 318), (15, 316), (16, 283), (17, 31…