プログラムでの値型の精度とか ― 2013年02月15日 11:33
HDF5DotNetとは別でプログラムの話。
プログラムで、多項式近似の係数を出力する機能がほしくなったので、適当にネットで転がっている情報をもとに作成。参考ページとできたのがこれ
エクセルを用いた最小二乗法 : http://homepage1.nifty.com/gfk/square_ren.htm
逆行列の求め方 : http://thira.plavox.info/blog/2008/06/_c.html
プログラム
これは、浮動小数点型を使用して計算を行うとどうしても誤差がでるらしい。
http://dobon.net/vb/dotnet/beginner/floatingpointerror.html
多少誤差が出ようが、似たような計算結果になってほしいところだが、実際に上記のプログラムを使用して算出された値を使い、使用したx値を与えてやっても同じようなy値が算出できなかった。
なので、結局エクセルのLinEstクラスを使用するようにした。
使用するには、HDF5DotNetの時と同じく、参照の追加を行わなければならない。追加するものは、COMにある"Microsoft Office xx(xxはofficeのバージョン)Object Library"。これを追加すると、ソリューションエクスプローラーの中の参照設定の中に"Microsoft.Office.Core"と"Microsoft.Office.Interop.Excel"が追加される。
これでエクセルの編集などができるようになったが、あくまで目的はワークシート関数の"LinEst"を使用すること。
とりあえず、簡単に以下のラッパークラスを作成した。
データを持ってきてLinEst関数を使用する。ただ単にラップしただけ。
あとはこれを使うだけ
プログラムで、多項式近似の係数を出力する機能がほしくなったので、適当にネットで転がっている情報をもとに作成。参考ページとできたのがこれ
エクセルを用いた最小二乗法 : http://homepage1.nifty.com/gfk/square_ren.htm
逆行列の求め方 : http://thira.plavox.info/blog/2008/06/_c.html
プログラム
//行列の作成とりあえずテストしてみて、エクセルのLINEST関数の結果と照らし合わせるなどして比較。いけそうだと思ったがうまくいかない場合がでてきた。その場合とは小数点の値を使う場合。たとえば、x = 0.1、0.2、0.3 を使用しての4次元の多項式y=a * x^4 + b * x^3 + c * x^2 + d * x^1 + e の各a,b,c,d,eを求めるために、各項を2乗、3乗としていったが、デバッグで値を見ると、後ろに妙な端数がある。
dimWithIntercept = 5; double[ , ] matrix = new double[ dimWithIntercept, dimWithIntercept]; double[ , ] copy = new double[ dimWithIntercept, dimWithIntercept ]; double[ , ] revMatrix = new double[ dimWithIntercept, dimWithIntercept ]; double[] vector = new double[dimWithIntercept]; List<double> ret = new List<double>( ); int digit = 15; MidpointRounding rounding = MidpointRounding.AwayFromZero; for( int i = 0 ; i < xyValues.Count ; i++ ) { // 行列の作成 for( int j = 0 ; j < dimWithIntercept ; j++ ) { for( int k = 0 ; k < dimWithIntercept ; k++ ) { matrix[ j, k ] = matrix[ j, k ] + Math.Pow( xyValues[ i ].Key, j ); copy[ j, k ] = matrix[ j, k ]; } //ベクトルの作成 vector[ j ] = vector[ j ] + Math.Pow(xyValues[ i ].Key, j ) ; } } // 逆行列の作成 // まずは単位行列を作る for( int i = 0 ; i < dimWithIntercept ; i++ ) { for( int j = 0 ; j < dimWithIntercept ; j++ ) { revMatrix[ i,j ] = (i == j) ? 1.0 : 0.0; } }
// 吐き出し法 double buf; for( int i = 0 ; i < dimWithIntercept ; i++ ) { // 単位化するために、対になる値で割る buf = copy[ i, i ]; // iの行を単位化するためにbufで割る for( int j = 0 ; j < dimWithIntercept ; j++ ) { copy[ i, j ] = copy[ i, j ] / buf; revMatrix[ i, j ] = revMatrix[ i, j ] / buf; } for( int j = 0 ; j < dimWithIntercept ; j++ ) {
// iと同じ行では計算しない if( i != j ) { buf = copy[ j, i ]; //n列目の値を0にする(n,nの場所はスルー) for( int k = 0 ; k < dimWithIntercept ; k++ ) { double a = copy[ i, k ] * buf; copy[ j, k ] = copy[ j, k ] - a;
a = revMatrix[ i, k ] * buf; revMatrix[ j, k ] =revMatrix[ j, k ] - a;
} }
} }
これは、浮動小数点型を使用して計算を行うとどうしても誤差がでるらしい。
http://dobon.net/vb/dotnet/beginner/floatingpointerror.html
多少誤差が出ようが、似たような計算結果になってほしいところだが、実際に上記のプログラムを使用して算出された値を使い、使用したx値を与えてやっても同じようなy値が算出できなかった。
なので、結局エクセルのLinEstクラスを使用するようにした。
使用するには、HDF5DotNetの時と同じく、参照の追加を行わなければならない。追加するものは、COMにある"Microsoft Office xx(xxはofficeのバージョン)Object Library"。これを追加すると、ソリューションエクスプローラーの中の参照設定の中に"Microsoft.Office.Core"と"Microsoft.Office.Interop.Excel"が追加される。
これでエクセルの編集などができるようになったが、あくまで目的はワークシート関数の"LinEst"を使用すること。
とりあえず、簡単に以下のラッパークラスを作成した。
static public class CoefficientsClass { static Excel.Application oXL = new Excel.Application( ); static Excel.WorksheetFunction funk = oXL.WorksheetFunction;説明するとstaticでエクセル起動とワークシート関数をクラスに置いておく。
public static Array GetCoefficients(Array y, Array x, bool a, bool b) { System.Object coefficients = funk.LinEst( y, x, a, b ); Array coeffArray = (Array)coefficients; return coeffArray; } }
データを持ってきてLinEst関数を使用する。ただ単にラップしただけ。
あとはこれを使うだけ
double[ , ] x = new double[ dim , xyValues.Count ]; double[] y = new double[ xyValues.Count ]; for( int i = 0 ; i < xyValues.Count ; i++ ) { for( int j = 0 ; j < dim ; j++ ) { x[ j, i ] = Math.Round(Math.Pow(xyValues[i].Key,j + 1),xを配列単体で行うとy=ax+bの係数と切片が得られる。xを上記のように二次配列にして、ほしい次元まで乗数倍したx値を用意すれば、多項近似式も得ることができる。
15,MidpointRounding.AwayFromZero); } y[ i ] = xyValues[ i ].Value; } Array coeffArray = CoefficientsClass.GetCoefficients( y, x ,true,false); List<double> ret = new List<double>(); for(int i = 1 ; i < coeffArray.Length + 1 ; i++){ ret.Add( (double)coeffArray.GetValue( i ) ); }
コメント
_ Anna ― 2013年12月29日 00:15
Which year are you in? http://www.inspiralia.com/vpxl/ buy cheap vpxl For claims with a DENY or PEND status, this column indicates the NYS Medicaid edit
_ Lauren ― 2013年12月29日 00:29
Whereabouts are you from? http://www.kitsunesuki.com/aralen/ buy chloroquine online Able to explain and depth with no
_ Ariana ― 2013年12月29日 00:29
The line's engaged http://www.doubledtrailers.com/singulair/ order singulair available when indicated, but WHO-approved generic drugs are preferred in an effort to
_ Angel ― 2013年12月29日 00:29
This is the job description http://www.doubledtrailers.com/singulair/ purchase montelukast prescribing date and the date of dispensing.
_ Molly ― 2013年12月29日 00:29
Insufficient funds http://www.lattery.com/aldactone/ aldactone online DUR Response Segment Chart D DUR Response Segment Chart D
_ Gianna ― 2013年12月29日 01:11
I'm not working at the moment http://nationalemsacademy.com/nitroglycerin/ spray nitroglycerin Or Pend Message Code (Table 10)
_ Tony ― 2013年12月29日 01:25
Have you seen any good films recently? http://motuweb.com/eskalith/ eskalith cr 450 field. If that ETIN was not on the
_ Audrey ― 2013年12月29日 01:25
I've come to collect a parcel http://www.nriol.com/zantac/ ranitidine online on all his/her patients
_ Gracie ― 2013年12月29日 01:25
Where are you calling from? http://motuweb.com/eskalith/ buy lithium carbonate online (if applicable), and legal and regulatory standards.
_ Hailey ― 2013年12月29日 02:06
I'll put her on http://www.dentalwellness4u.com/geodon/ buy ziprasidone online Purdue Kenya Program Waiver of Liability Policy
_ Kimberly ― 2013年12月29日 02:06
I'm from England http://www.gordonswine.com/proventil/ order proventil receive this credit, a preceptor must have significant input into the precepting and education of a given
_ Nathaniel ― 2013年12月29日 02:06
An envelope http://www.womenpriests.org/zantac/ buy zantac and NO CLAIM TO FAÂ (field 503-F3) will be
_ Hunter ― 2013年12月29日 02:17
Could I ask who's calling? http://colheradacultural.com.br/elimite/ where can i buy elimite to have this protection because the University cannot be responsible for any expenses or losses
_ Sean ― 2013年12月29日 02:18
Enter your PIN http://colheradacultural.com.br/elimite/ scabies permethrin the Healthcare only; refuses to relationships; avoids relationships; participates; team as an integral member;
_ Sarah ― 2013年12月29日 02:18
I'm retired http://www.kirnberger.com/cytotec/ misoprostol and mifepristone special priviledges. abuses special actions. Occasionally abuses special abuses special
_ Kaitlyn ― 2013年12月29日 02:57
Where do you come from? http://www.doubledtrailers.com/alesse/ alesse 28 Co-Ordination of Benefits COB 18, 27, 35, 55 Incorrect Date of Birth 15, 54
_ Diego ― 2013年12月29日 02:57
Another year http://www.boholplazaresort.com/flagyl/ flagyl er 400 mg 2.1 Assume safe and accurate compounding, preparation and dispensing of medication and
_ Angel ― 2013年12月29日 03:10
We need someone with qualifications http://www.edlaw.org/ampicillin/ ampicillin 500 Identify ways to resolve incorrect medication orders.
_ Kaitlyn ― 2013年12月29日 03:50
I've got a very weak signal http://sanven.es/artane/ artane 1 mg medication-related problems medication history. medications (herbals, patient medication history.
_ Kayla ― 2013年12月29日 03:50
I'll put her on http://www.bondinho.com.br/clonidine/ clonidine hydrochloride The next placement (#9) attempts to match choices 1-7 for all students.
_ rikky ― 2013年12月29日 03:50
Canada>Canada http://www.bondinho.com.br/clonidine/ what is clonidine Details of infusion bag size e.g. 250ml or 500ml
_ Alexander ― 2013年12月29日 03:50
I'm interested in this position http://www.csrsafety.com/tricor/ fenofibrate 160 mg relationships between or treatment of diverse
_ Carlos ― 2013年12月29日 03:50
Will I be paid weekly or monthly? http://www.fieldworkfuture.com/index.php/dilantin/ dilantin online Put the peas through a vegetable mill or sieve to make a puree.
_ Madison ― 2013年12月29日 04:01
I'd like to send this parcel to http://www.rockygrove.com/floxin/ ofloxacin online Page 11 of 111
_ Emily ― 2013年12月29日 04:42
I'd like to open a business account http://www.kitsunesuki.com/artane/ artane tablets via the Additional Message field. If the recipient's other insurance covers drugs, either K,
_ Xavier ― 2013年12月29日 04:42
I'm afraid that number's ex-directory http://www.kitsunesuki.com/artane/ buy trihexyphenidyl online 5.8. Assess and manage the requiring urgent versus
_ Eric ― 2013年12月29日 04:52
When do you want me to start? http://www.delpiano.com/actos/ generic actos order entry with your preceptor. If available at your institution,
_ nogood87 ― 2013年12月29日 04:52
How much does the job pay? http://www.lafuga.net/cytotec/ diclofenac and misoprostol available for CSP applicants. At this point, ANY type of CSP position that has more than one position
_ Mike ― 2013年12月29日 04:52
A packet of envelopes http://elliottworkgroup.com/imitrex/ imitrex injection price 1/2 cup CARROTS in very, very thin slices
_ dirtbill ― 2013年12月29日 04:52
We'd like to invite you for an interview http://nationalemsacademy.com/maxalt/ maxalt 10mg I simply believe now that as an individual and as a teacher, the concept of self-
_ Alexandra ― 2013年12月29日 05:33
I'm sorry, I'm not interested http://www.manuelle-gautrand.com/combivent/ combivent price development of the competencies required of the graduate, and the college or school
_ Layla ― 2013年12月29日 05:33
I'm interested in this position http://www.dymedix.com/vermox/ generic mebendazole 8) CPR Training - All students are required to complete training and become certified in adult
_ Victoria ― 2013年12月29日 05:33
I'm a housewife http://www.edlaw.org/protonix/ order pantoprazole clean your room, please leave the door to your room open. If your door is shut, they will know that you do not wish
_ Olivia ― 2013年12月29日 05:42
Another year http://www.womenpriests.org/amitriptyline/ amitriptyline hcl 10 mg migraines Week 2 Order entry Work with purchasing person Do medication histories and/or Intro to IVs ± watch aseptic Review aseptic technique
_ Blake ― 2013年12月29日 06:23
How much were you paid in your last job? http://www.huddlestontax.com/macrobid/ macrobid antibiotic are clearly explained. During the conclusion, the discussion points are clearly
_ Brian ― 2013年12月29日 06:23
I'm sorry, she's http://www.huddlestontax.com/macrobid/ macrobid dose for supervision, assistance or help). Complete all tasks to the best of your ability. Complete them accurately, thoroughly, legibly, and in a Commit to excellence in your continued professional development (CPD) ² this starts during school.
_ lifestile ― 2013年12月29日 06:23
Thanks funny site http://www.wataganpark.com.au/actos/ buy pioglitazone Laxatives, bulk producers and stool softeners
_ Alexa ― 2013年12月29日 06:31
I'm not working at the moment http://www.womenpriests.org/dostinex/ order dostinex online Treat others kindly and with compassion, whether in the classroom setting, in meetings, or on rotation.
_ Luke ― 2013年12月29日 06:31
I'm sorry, she's http://www.dentalwellness4u.com/vytorin/ buy vytorin your team is caring for.
_ Austin ― 2013年12月29日 07:13
Other amount http://www.rockygrove.com/ditropan/ buy oxybutynin online 16. Dispense and verify prescriptions for completeness and accuracy.
_ Anthony ― 2013年12月29日 07:13
I do some voluntary work http://www.boholplazaresort.com/lopid/ lopid tablets specialized services (immunizations, limited prescribing, etc.) will need to do more than simply fulfill their
_ Isabelle ― 2013年12月29日 07:13
How do I get an outside line? http://www.rh-partners.com/endep/ endep 25 mg medications and medical products.
_ Magic ― 2013年12月29日 07:13
I was made redundant two months ago http://www.hotelcile.me/requip/ renova cream uk secondary identification such as a student ID number. In this case, the cardholder number is not shown on the front of the card.
_ Miguel ― 2013年12月29日 07:21
I'm unemployed http://casarusia.com/cytotec/ cytotec 200 microgram tablets professionals respectfully and courteously.
_ Jackson ― 2013年12月29日 08:02
I love the theatre http://www.earsc.org/prednisone/ cheap prednisone packages are converting your 5
_ Ella ― 2013年12月29日 08:02
It's OK http://www.earsc.org/atrovent/ order atrovent If a claim is captured but cannot be processed for
_ Sofia ― 2013年12月29日 08:02
Can I call you back? http://www.earsc.org/prednisone/ cheap prednisone apply in New Brunswick, Newfoundland, Nova Scotia and Prince Edward Island)
_ Madeline ― 2013年12月29日 08:02
I'm only getting an answering machine http://www.boholplazaresort.com/ventolin/ buy cheap ventolin online The Pro-DUR/ECCA online system is an adjudication system. The dollar amount returned
_ Ethan ― 2013年12月29日 08:10
A law firm http://www.cancerlifecenter.org/toprol/ lopressor toprol xl credentials, experience and professional development will be needed. In this ever changing healthcare
_ Lucas ― 2013年12月29日 08:10
real beauty page http://www.dymedix.com/stopicsrax/ suprax 400 mg tablet ¥Status of color/B&W switch
※コメントの受付件数を超えているため、この記事にコメントすることができません。
トラックバック
※トラックバックの受付件数を超えているため、この記事にトラックバックを投稿することができません。
_ セイコー 腕時計 - 2013年06月16日 17:18
このような情報についての興味深い記事を見つける素晴らしいです。それは宝物を見つけるようなものだ。私はあなたのビューで多くのポイントと共有をどう表現する感謝しています。ありがとう。
_ citizen - 2013年06月16日 17:18
他の素敵なポストをありがとう。どこで、誰が書いているような理想的なアプローチで情報のようなものを得ることができる?私は、プレゼンテーション、その後の週を持っている、と私はそのような情報を検索しに行きます。
_ セイコー - 2013年06月16日 17:19
これはblog.Reallyあなたに感謝素晴らしいものです!グレート。
_ adidas スニーカー - 2013年06月16日 17:19
これはblog.Reallyあなたに感謝素晴らしいものです!グレート。
_ ティファニー ネックレス - 2013年06月16日 17:19
他の素敵なポストをありがとう。どこで、誰が書いているような理想的なアプローチで情報のようなものを得ることができる?私は、プレゼンテーション、その後の週を持っている、と私はそのような情報を検索しに行きます。
_ オークリー サングラス - 2013年06月16日 17:19
すげえねえ。 テーマの種類を使っていますか?私はそれがあまりにも私のブログで使用したい。
_ コーチ メンズ - 2013年06月16日 17:19
これはblog.Reallyあなたに感謝素晴らしいものです!グレート。
_ ブライトリング - 2013年06月16日 17:19
このウェブサイトには、本当にウォークスルーこれについて思ったのすべての情報のためであり、誰が頼むのか分からなかった。ここに垣間見る、とあなたは間違いなくそれを発見するでしょう。
_ オークリー サングラス - 2013年06月16日 17:19
他の素敵なポストをありがとう。どこで、誰が書いているような理想的なアプローチで情報のようなものを得ることができる?私は、プレゼンテーション、その後の週を持っている、と私はそのような情報を検索しに行きます。
_ レイバン サングラス - 2013年06月16日 17:19
後すぐにあなたのウェブサイト内のブログの記事の数だけを調べて、私は実際にあなたのブログの方法のように。私は私のブックマークのウェブサイトレコードにブックマークを付け、すぐに再度確認することができます。 Plsは私だけでなく私のウェブサイトを試してみて、あなたが信じているものを私を知っている許可します。
_ ブルガリ 時計 - 2013年06月16日 17:19
すげえねえ。 テーマの種類を使っていますか?私はそれがあまりにも私のブログで使用したい。
_ 腕時計 メンズ - 2013年06月16日 17:19
すげえねえ。 テーマの種類を使っていますか?私はそれがあまりにも私のブログで使用したい。
_ プラダ バッグ - 2013年06月16日 17:19
このウェブサイトには、本当にウォークスルーこれについて思ったのすべての情報のためであり、誰が頼むのか分からなかった。ここに垣間見る、とあなたは間違いなくそれを発見するでしょう。
_ フェラガモ 靴 - 2013年07月06日 23:08
プログラムでの値型の精度とか: プログラムなど個人的メモ
_ lunettes oakley - 2013年08月29日 14:46
lunettes oakley
_ finalfantasyxivhelp.com - 2015年04月09日 15:12
That makes two of us.
_ sell ffxiv gil - 2015年11月21日 15:30
You have a good taste.
_ final fantasy xiv gil - 2015年11月22日 09:44
You started it !
_ fut 16 coins - 2015年11月24日 11:37
Too bad we must return them.
_ cheapest eso power leveling - 2015年11月25日 05:56
Why do not we try this ?
_ cheap fifa coins - 2015年11月27日 22:31
You did a good job .
_ nfl 16 coins ps3 buy - 2016年01月02日 05:21
You are the savior of my life.
_ Madden 16 Coins - 2016年01月02日 16:46
No, I know.
_ Cheap MUT Coins - 2016年01月04日 13:43
You have a good taste.
_ fifa 16 coins PC - 2016年01月07日 07:05
That's not the issue.
_ fifa coins - 2016年01月13日 10:21
How come you are working here?
_ Runescape Gold - 2016年04月12日 04:27
Be good!
_ www.RunescapeGold2007.com - 2016年04月12日 23:37
You did it !
_ buy BNS gold - 2016年04月24日 07:14
I don't have the slightest idea
_ buy blade and soul gold - 2016年04月25日 08:15
Up yours!
_ FIFA16Mall FIFA 16 Coins - 2016年05月21日 03:52
You are the savior of my life.
_ www.fifa16mall.com - 2016年05月21日 08:06
Take my word for it.
_ fifa 17 coins - 2016年05月29日 19:08
You are looking sharp !
_ u4fifa - 2016年05月30日 03:43
You bet !
_ buy fifa 17 coins - 2016年05月30日 07:59
You did it !
_ u4fifa - 2016年05月30日 11:31
Im very pleased with your work.
_ u4fifa reviews - 2016年05月30日 17:49
look great today.
_ u4fifa - 2016年05月30日 19:00
this website can help me to locate some good suggestions!
_ view - 2016年06月25日 14:37
You're very eloquent.
_ buy cheap fifa 17 coins - 2016年07月03日 10:04
Not that I know of.
_ cheap fifa 17 coins - 2016年07月04日 10:57
The youre very professional .
_ cheap fifa 17 coins - 2016年07月04日 13:56
That's not the issue.
_ u4fifa - 2016年07月07日 03:31
The youre really talented .
_ U4FIFA & FIFA 17 News - 2016年07月07日 06:56
this is a very useful mmorpg web site!
_ u4fifa - 2016年07月07日 13:56
you have a good sense of humor.
_ cheap fifa 17 coins - 2016年07月07日 16:39
You're very eloquent.
_ fut 17 coins - 2016年07月14日 20:38
The youre really talented .
_ u4fifa.com - 2016年07月15日 06:24
You always love it.
_ buy fifa 17 pc coins - 2016年07月17日 02:51
Come on, cut it out!
_ fifa 17 coins - 2016年07月18日 06:01
No one can do it better than you.
最近のコメント