PageRenderTime 71ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/binfilter/bf_sc/source/core/tool/sc_interpr5.cxx

https://bitbucket.org/mst/ooo340
C++ | 4067 lines | 3940 code | 65 blank | 62 comment | 913 complexity | d9a3bdb898e42fb5e9216d11204001b5 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, BSD-3-Clause-No-Nuclear-License-2014, GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * Copyright 2000, 2010 Oracle and/or its affiliates.
  6. *
  7. * OpenOffice.org - a multi-platform office productivity suite
  8. *
  9. * This file is part of OpenOffice.org.
  10. *
  11. * OpenOffice.org is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License version 3
  13. * only, as published by the Free Software Foundation.
  14. *
  15. * OpenOffice.org is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License version 3 for more details
  19. * (a copy is included in the LICENSE file that accompanied this code).
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * version 3 along with OpenOffice.org. If not, see
  23. * <http://www.openoffice.org/license.html>
  24. * for a copy of the LGPLv3 License.
  25. *
  26. ************************************************************************/
  27. #ifdef PCH
  28. #endif
  29. #ifdef _MSC_VER
  30. #pragma hdrstop
  31. #endif
  32. // INCLUDE ---------------------------------------------------------------
  33. #include <string.h>
  34. //#include <math.h>
  35. #ifndef _ZFORLIST_HXX //autogen
  36. #include <bf_svtools/zforlist.hxx>
  37. #endif
  38. #include "interpre.hxx"
  39. #include "dociter.hxx"
  40. #include "scmatrix.hxx"
  41. #include "globstr.hrc"
  42. namespace binfilter {
  43. // STATIC DATA -----------------------------------------------------------
  44. #define SCdEpsilon 1.0E-7
  45. // -----------------------------------------------------------------------
  46. double ScInterpreter::ScGetGGT(double fx, double fy)
  47. {
  48. if (fy == 0.0 || fx == 0.0)
  49. {
  50. SetError(errIllegalArgument);
  51. return 1.0;
  52. }
  53. else
  54. {
  55. double fz = fmod(fx, fy);
  56. while (fz > 0.0)
  57. {
  58. fx = fy;
  59. fy = fz;
  60. fz = fmod(fx, fy);
  61. }
  62. return fy;
  63. }
  64. }
  65. void ScInterpreter::ScGGT()
  66. {
  67. BYTE nParamCount = GetByte();
  68. if ( MustHaveParamCountMin( nParamCount, 1 ) )
  69. {
  70. double fSign = 1.0;
  71. double fx, fy = 0.0;
  72. switch (GetStackType())
  73. {
  74. case svDouble :
  75. case svString:
  76. case svSingleRef:
  77. {
  78. fy = GetDouble();
  79. if (fy < 0.0)
  80. {
  81. fy *= -1.0;
  82. fSign *= -1.0;
  83. }
  84. }
  85. break;
  86. case svDoubleRef :
  87. {
  88. ScRange aRange;
  89. USHORT nErr = 0;
  90. PopDoubleRef( aRange );
  91. double nCellVal;
  92. ScValueIterator aValIter(pDok, aRange, glSubTotal);
  93. if (aValIter.GetFirst(nCellVal, nErr))
  94. {
  95. fy = nCellVal;
  96. if (fy < 0.0)
  97. {
  98. fy *= -1.0;
  99. fSign *= -1.0;
  100. }
  101. while (nErr == 0 && aValIter.GetNext(nCellVal, nErr))
  102. {
  103. fx = nCellVal;
  104. if (fx < 0.0)
  105. {
  106. fx *= -1.0;
  107. fSign *= -1.0;
  108. }
  109. fy = ScGetGGT(fx, fy);
  110. }
  111. SetError(nErr);
  112. }
  113. else
  114. SetError(errIllegalArgument);
  115. }
  116. break;
  117. case svMatrix :
  118. {
  119. ScMatrix* pMat = PopMatrix();
  120. if (pMat)
  121. {
  122. USHORT nC, nR;
  123. pMat->GetDimensions(nC, nR);
  124. if (nC == 0 || nR == 0)
  125. SetError(errIllegalArgument);
  126. else
  127. {
  128. if (!pMat->IsValue(0))
  129. {
  130. SetIllegalArgument();
  131. return;
  132. }
  133. fy = pMat->GetDouble(0);
  134. if (fy < 0.0)
  135. {
  136. fy *= -1.0;
  137. fSign *= -1.0;
  138. }
  139. ULONG nCount = (ULONG) nC * nR;
  140. for ( ULONG j = 1; j < nCount; j++ )
  141. {
  142. if (!pMat->IsValue(j))
  143. {
  144. SetIllegalArgument();
  145. return;
  146. }
  147. fx = pMat->GetDouble(j);
  148. if (fx < 0.0)
  149. {
  150. fx *= -1.0;
  151. fSign *= -1.0;
  152. }
  153. fy = ScGetGGT(fx, fy);
  154. }
  155. }
  156. }
  157. }
  158. break;
  159. default : SetError(errIllegalParameter); break;
  160. }
  161. ScRange aRange;
  162. for (short i = 0; i < (short) nParamCount - 1; i++)
  163. {
  164. switch (GetStackType())
  165. {
  166. case svDouble :
  167. case svString:
  168. case svSingleRef:
  169. {
  170. fx = GetDouble();
  171. if (fx < 0.0)
  172. {
  173. fx *= -1.0;
  174. fSign *= -1.0;
  175. }
  176. fy = ScGetGGT(fx, fy);
  177. }
  178. break;
  179. case svDoubleRef :
  180. {
  181. USHORT nErr = 0;
  182. PopDoubleRef( aRange );
  183. double nCellVal;
  184. ScValueIterator aValIter(pDok, aRange, glSubTotal);
  185. if (aValIter.GetFirst(nCellVal, nErr))
  186. {
  187. fx = nCellVal;
  188. if (fx < 0.0)
  189. {
  190. fx *= -1.0;
  191. fSign *= -1.0;
  192. }
  193. fy = ScGetGGT(fx, fy);
  194. while (nErr == 0 && aValIter.GetNext(nCellVal, nErr))
  195. {
  196. fx = nCellVal;
  197. if (fx < 0.0)
  198. {
  199. fx *= -1.0;
  200. fSign *= -1.0;
  201. }
  202. fy = ScGetGGT(fx, fy);
  203. }
  204. SetError(nErr);
  205. }
  206. else
  207. SetError(errIllegalArgument);
  208. }
  209. break;
  210. case svMatrix :
  211. {
  212. ScMatrix* pMat = PopMatrix();
  213. if (pMat)
  214. {
  215. USHORT nC, nR;
  216. pMat->GetDimensions(nC, nR);
  217. if (nC == 0 || nR == 0)
  218. SetError(errIllegalArgument);
  219. else
  220. {
  221. if (!pMat->IsValue(0))
  222. {
  223. SetIllegalArgument();
  224. return;
  225. }
  226. fx = pMat->GetDouble(0);
  227. if (fx < 0.0)
  228. {
  229. fx *= -1.0;
  230. fSign *= -1.0;
  231. }
  232. fy = ScGetGGT(fx, fy);
  233. ULONG nCount = (ULONG) nC * nR;
  234. for ( ULONG j = 1; j < nCount; j++ )
  235. {
  236. if (!pMat->IsValue(j))
  237. {
  238. SetIllegalArgument();
  239. return;
  240. }
  241. fx = pMat->GetDouble(j);
  242. if (fx < 0.0)
  243. {
  244. fx *= -1.0;
  245. fSign *= -1.0;
  246. }
  247. fy = ScGetGGT(fx, fy);
  248. }
  249. }
  250. }
  251. }
  252. break;
  253. default : SetError(errIllegalParameter); break;
  254. }
  255. }
  256. if (fSign == -1.0)
  257. PushDouble(-fy);
  258. else
  259. PushDouble(fy);
  260. }
  261. }
  262. void ScInterpreter:: ScKGV()
  263. {
  264. BYTE nParamCount = GetByte();
  265. if ( MustHaveParamCountMin( nParamCount, 1 ) )
  266. {
  267. double fSign = 1.0;
  268. double fx, fy = 0.0;
  269. switch (GetStackType())
  270. {
  271. case svDouble :
  272. case svString:
  273. case svSingleRef:
  274. {
  275. fy = GetDouble();
  276. if (fy < 0.0)
  277. {
  278. fy *= -1.0;
  279. fSign *= -1.0;
  280. }
  281. }
  282. break;
  283. case svDoubleRef :
  284. {
  285. ScRange aRange;
  286. USHORT nErr = 0;
  287. PopDoubleRef( aRange );
  288. double nCellVal;
  289. ScValueIterator aValIter(pDok, aRange, glSubTotal);
  290. if (aValIter.GetFirst(nCellVal, nErr))
  291. {
  292. fy = nCellVal;
  293. if (fy < 0.0)
  294. {
  295. fy *= -1.0;
  296. fSign *= -1.0;
  297. }
  298. while (nErr == 0 && aValIter.GetNext(nCellVal, nErr))
  299. {
  300. fx = nCellVal;
  301. if (fx < 0.0)
  302. {
  303. fx *= -1.0;
  304. fSign *= -1.0;
  305. }
  306. fy = fx * fy / ScGetGGT(fx, fy);
  307. }
  308. SetError(nErr);
  309. }
  310. else
  311. SetError(errIllegalArgument);
  312. }
  313. break;
  314. case svMatrix :
  315. {
  316. ScMatrix* pMat = PopMatrix();
  317. if (pMat)
  318. {
  319. USHORT nC, nR;
  320. pMat->GetDimensions(nC, nR);
  321. if (nC == 0 || nR == 0)
  322. SetError(errIllegalArgument);
  323. else
  324. {
  325. if (!pMat->IsValue(0))
  326. {
  327. SetIllegalArgument();
  328. return;
  329. }
  330. fy = pMat->GetDouble(0);
  331. if (fy < 0.0)
  332. {
  333. fy *= -1.0;
  334. fSign *= -1.0;
  335. }
  336. ULONG nCount = (ULONG) nC * nR;
  337. for ( ULONG j = 1; j < nCount; j++ )
  338. {
  339. if (!pMat->IsValue(j))
  340. {
  341. SetIllegalArgument();
  342. return;
  343. }
  344. fx = pMat->GetDouble(j);
  345. if (fx < 0.0)
  346. {
  347. fx *= -1.0;
  348. fSign *= -1.0;
  349. }
  350. fy = fx * fy / ScGetGGT(fx, fy);
  351. }
  352. }
  353. }
  354. }
  355. break;
  356. default : SetError(errIllegalParameter); break;
  357. }
  358. ScRange aRange;
  359. for (short i = 0; i < (short) nParamCount - 1; i++)
  360. {
  361. switch (GetStackType())
  362. {
  363. case svDouble :
  364. case svString:
  365. case svSingleRef:
  366. {
  367. fx = GetDouble();
  368. if (fx < 0.0)
  369. {
  370. fx *= -1.0;
  371. fSign *= -1.0;
  372. }
  373. fy = fx * fy / ScGetGGT(fx, fy);
  374. }
  375. break;
  376. case svDoubleRef :
  377. {
  378. USHORT nErr = 0;
  379. PopDoubleRef( aRange );
  380. double nCellVal;
  381. ScValueIterator aValIter(pDok, aRange, glSubTotal);
  382. if (aValIter.GetFirst(nCellVal, nErr))
  383. {
  384. fx = nCellVal;
  385. if (fx < 0.0)
  386. {
  387. fx *= -1.0;
  388. fSign *= -1.0;
  389. }
  390. fy = fx * fy / ScGetGGT(fx, fy);
  391. while (nErr == 0 && aValIter.GetNext(nCellVal, nErr))
  392. {
  393. fx = nCellVal;
  394. if (fx < 0.0)
  395. {
  396. fx *= -1.0;
  397. fSign *= -1.0;
  398. }
  399. fy = fx * fy / ScGetGGT(fx, fy);
  400. }
  401. SetError(nErr);
  402. }
  403. else
  404. SetError(errIllegalArgument);
  405. }
  406. break;
  407. case svMatrix :
  408. {
  409. ScMatrix* pMat = PopMatrix();
  410. if (pMat)
  411. {
  412. USHORT nC, nR;
  413. pMat->GetDimensions(nC, nR);
  414. if (nC == 0 || nR == 0)
  415. SetError(errIllegalArgument);
  416. else
  417. {
  418. if (!pMat->IsValue(0))
  419. {
  420. SetIllegalArgument();
  421. return;
  422. }
  423. fx = pMat->GetDouble(0);
  424. if (fx < 0.0)
  425. {
  426. fx *= -1.0;
  427. fSign *= -1.0;
  428. }
  429. fy = fx * fy / ScGetGGT(fx, fy);
  430. ULONG nCount = (ULONG) nC * nR;
  431. for ( ULONG j = 1; j < nCount; j++ )
  432. {
  433. if (!pMat->IsValue(j))
  434. {
  435. SetIllegalArgument();
  436. return;
  437. }
  438. fx = pMat->GetDouble(j);
  439. if (fx < 0.0)
  440. {
  441. fx *= -1.0;
  442. fSign *= -1.0;
  443. }
  444. fy = fx * fy / ScGetGGT(fx, fy);
  445. }
  446. }
  447. }
  448. }
  449. break;
  450. default : SetError(errIllegalParameter); break;
  451. }
  452. }
  453. if (fSign == -1.0)
  454. PushDouble(-fy);
  455. else
  456. PushDouble(fy);
  457. }
  458. }
  459. /*N*/ ScMatrix* ScInterpreter::GetNewMat(USHORT nC, USHORT nR, USHORT& nMatInd)
  460. /*N*/ {
  461. /*N*/ if (nMatCount == MAX_ANZ_MAT)
  462. /*N*/ {
  463. /*N*/ DBG_ERROR("ScInterpreter::GetNewMat: Matrixueberlauf");
  464. /*N*/ SetError(errCodeOverflow);
  465. /*N*/ nMatInd = MAX_ANZ_MAT;
  466. /*N*/ return NULL;
  467. /*N*/ }
  468. /*N*/ else
  469. /*N*/ {
  470. /*N*/ if (!bMatDel) // beim ersten Mal
  471. /*N*/ {
  472. /*N*/ ppTempMatArray = new ScMatrix* [MAX_ANZ_MAT];
  473. /*N*/ for (USHORT i = 0; i < MAX_ANZ_MAT; i++)
  474. /*N*/ ppTempMatArray[i] = NULL;
  475. /*N*/ bMatDel = TRUE;
  476. /*N*/ }
  477. /*N*/ ppTempMatArray[nMatCount] = new ScMatrix(nC, nR);
  478. /*N*/ nMatInd = nMatCount++;
  479. /*N*/ return ppTempMatArray[nMatInd];
  480. /*N*/ }
  481. /*N*/ }
  482. /*N*/ void ScInterpreter::ResetNewMat(USHORT nIndex)
  483. /*N*/ {
  484. /*N*/ if (nIndex < MAX_ANZ_MAT)
  485. /*N*/ {
  486. /*N*/ ppTempMatArray[nIndex] = NULL;
  487. /*N*/ if (nIndex == nMatCount - 1)
  488. /*N*/ nMatCount--;
  489. /*N*/ }
  490. /*N*/ }
  491. /*N*/ ScMatrix* ScInterpreter::GetMatrix(USHORT& nMatInd)
  492. /*N*/ {
  493. /*N*/ ScMatrix* pMat = NULL;
  494. /*N*/ switch (GetStackType())
  495. /*N*/ {
  496. /*?*/ case svSingleRef :
  497. /*?*/ {
  498. /*?*/ ScAddress aAdr;
  499. /*?*/ PopSingleRef( aAdr );
  500. /*?*/ pMat = GetNewMat(1, 1, nMatInd);
  501. /*?*/ if (pMat)
  502. /*?*/ {
  503. /*?*/ ScBaseCell* pCell = GetCell( aAdr );
  504. /*?*/ if ( pCell && pCell->GetCellType() != CELLTYPE_NOTE )
  505. /*?*/ {
  506. /*?*/ if (HasCellValueData(pCell))
  507. /*?*/ pMat->PutDouble(GetCellValue(aAdr, pCell), 0);
  508. /*?*/ else
  509. /*?*/ {
  510. /*?*/ String aStr;
  511. /*?*/ GetCellString(aStr, pCell);
  512. /*?*/ pMat->PutString(aStr, 0);
  513. /*?*/ }
  514. /*?*/ }
  515. /*?*/ else
  516. /*?*/ pMat->PutEmpty( 0 );
  517. /*?*/ }
  518. /*?*/ else
  519. /*?*/ SetError(errCodeOverflow);
  520. /*?*/ }
  521. /*?*/ break;
  522. /*N*/ case svDoubleRef:
  523. /*N*/ {
  524. /*N*/ USHORT nCol1, nRow1, nTab1, nCol2, nRow2, nTab2;
  525. /*N*/ PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
  526. /*N*/ if (nTab1 == nTab2)
  527. /*N*/ {
  528. /*N*/ USHORT i, j;
  529. /*N*/ if ( (ULONG) (nRow2 - nRow1 + 1) * (nCol2 - nCol1 + 1) >
  530. /*N*/ ScMatrix::GetElementsMax() )
  531. /*N*/ SetError(errStackOverflow);
  532. /*N*/ else
  533. /*N*/ {
  534. /*N*/ pMat = GetNewMat(nCol2 - nCol1 + 1, nRow2 - nRow1 + 1, nMatInd);
  535. /*N*/ if (pMat)
  536. /*N*/ {
  537. /*N*/ ScAddress aAdr( nCol1, nRow1, nTab1 );
  538. /*N*/ for (i = nRow1; i <= nRow2; i++)
  539. /*N*/ {
  540. /*N*/ aAdr.SetRow( i );
  541. /*N*/ for (j = nCol1; j <= nCol2; j++)
  542. /*N*/ {
  543. /*N*/ aAdr.SetCol( j );
  544. /*N*/ ScBaseCell* pCell = GetCell( aAdr );
  545. /*N*/ if ( pCell && pCell->GetCellType() != CELLTYPE_NOTE )
  546. /*N*/ {
  547. /*N*/ if (HasCellValueData(pCell))
  548. /*N*/ pMat->PutDouble(
  549. /*N*/ GetCellValue( aAdr, pCell ),
  550. /*N*/ j-nCol1, i-nRow1);
  551. /*N*/ else
  552. /*N*/ {
  553. /*?*/ String aStr;
  554. /*?*/ GetCellString(aStr, pCell);
  555. /*?*/ pMat->PutString(aStr, j-nCol1, i-nRow1);
  556. /*N*/ }
  557. /*N*/ }
  558. /*N*/ else
  559. /*?*/ pMat->PutEmpty( j-nCol1, i-nRow1 );
  560. /*N*/ }
  561. /*N*/ }
  562. /*N*/ }
  563. /*N*/ else
  564. /*N*/ SetError(errCodeOverflow);
  565. /*N*/ }
  566. /*N*/ }
  567. /*N*/ else // keine 2D-Matrix
  568. /*N*/ {
  569. /*N*/ nMatInd = MAX_ANZ_MAT;
  570. /*N*/ SetError(errIllegalParameter);
  571. /*N*/ }
  572. /*N*/ }
  573. /*N*/ break;
  574. /*N*/ case svMatrix:
  575. /*N*/ pMat = PopMatrix();
  576. /*N*/ nMatInd = MAX_ANZ_MAT;
  577. /*N*/ break;
  578. /*?*/ default:
  579. /*?*/ Pop();
  580. /*?*/ nMatInd = MAX_ANZ_MAT;
  581. /*?*/ SetError(errIllegalParameter);
  582. /*?*/ break;
  583. /*N*/ }
  584. /*N*/ return pMat;
  585. /*N*/ }
  586. void ScInterpreter::ScMatValue()
  587. {
  588. if ( MustHaveParamCount( GetByte(), 3 ) )
  589. {
  590. USHORT nR = (USHORT) ::rtl::math::approxFloor(GetDouble()); // 0 bis nAnz - 1
  591. USHORT nC = (USHORT) ::rtl::math::approxFloor(GetDouble()); // 0 bis nAnz - 1
  592. switch (GetStackType())
  593. {
  594. case svSingleRef :
  595. {
  596. ScAddress aAdr;
  597. PopSingleRef( aAdr );
  598. ScBaseCell* pCell = GetCell( aAdr );
  599. if (pCell && pCell->GetCellType() == CELLTYPE_FORMULA)
  600. {
  601. USHORT nErrCode = ((ScFormulaCell*)pCell)->GetErrCode();
  602. if (nErrCode != 0)
  603. {
  604. SetError(nErrCode);
  605. PushInt(0);
  606. }
  607. else
  608. {
  609. ScMatrix* pMat;
  610. ((ScFormulaCell*)pCell)->GetMatrix(&pMat);
  611. if (pMat)
  612. {
  613. USHORT nCl, nRw;
  614. pMat->GetDimensions(nCl, nRw);
  615. if (nC < nCl && nR < nRw)
  616. {
  617. BOOL bIsString;
  618. const MatValue* pMatVal = pMat->Get(nC, nR, bIsString);
  619. if (bIsString)
  620. PushString( pMatVal->GetString() );
  621. else
  622. PushDouble(pMatVal->fVal);
  623. }
  624. else
  625. SetNoValue();
  626. }
  627. else
  628. SetNoValue();
  629. }
  630. }
  631. else
  632. SetIllegalParameter();
  633. }
  634. break;
  635. case svDoubleRef :
  636. {
  637. USHORT nCol1, nRow1, nTab1, nCol2, nRow2, nTab2;
  638. PopDoubleRef(nCol1, nRow1, nTab1, nCol2, nRow2, nTab2);
  639. if (nCol2 - nCol1 >= nR && nRow2 - nRow1 >= nC && nTab1 == nTab2)
  640. {
  641. ScAddress aAdr( nCol1 + nR, nRow1 + nC, nTab1 );
  642. ScBaseCell* pCell = GetCell( aAdr );
  643. if (HasCellValueData(pCell))
  644. PushDouble(GetCellValue( aAdr, pCell ));
  645. else
  646. {
  647. String aStr;
  648. GetCellString(aStr, pCell);
  649. PushString(aStr);
  650. }
  651. }
  652. else
  653. SetNoValue();
  654. }
  655. break;
  656. case svMatrix:
  657. {
  658. ScMatrix* pMat = PopMatrix();
  659. if (pMat)
  660. {
  661. USHORT nCl, nRw;
  662. pMat->GetDimensions(nCl, nRw);
  663. if (nC < nCl && nR < nRw)
  664. {
  665. BOOL bIsString;
  666. const MatValue* pMatVal = pMat->Get(nC, nR, bIsString);
  667. if (bIsString)
  668. PushString( pMatVal->GetString() );
  669. else
  670. PushDouble(pMatVal->fVal);
  671. }
  672. else
  673. SetNoValue();
  674. }
  675. else
  676. SetNoValue();
  677. }
  678. break;
  679. default:
  680. Pop();
  681. SetIllegalParameter();
  682. break;
  683. }
  684. }
  685. }
  686. void ScInterpreter::ScEMat()
  687. {
  688. if ( MustHaveParamCount( GetByte(), 1 ) )
  689. {
  690. ULONG nDim = (ULONG) ::rtl::math::approxFloor(GetDouble());
  691. if ( nDim * nDim > ScMatrix::GetElementsMax() || nDim == 0)
  692. SetIllegalArgument();
  693. else
  694. {
  695. USHORT nMatInd;
  696. ScMatrix* pRMat = GetNewMat((USHORT)nDim, (USHORT)nDim, nMatInd);
  697. if (pRMat)
  698. {
  699. MEMat(pRMat, (USHORT) nDim);
  700. nRetMat = nMatInd;
  701. PushMatrix(pRMat);
  702. }
  703. else
  704. SetError(errStackOverflow);
  705. }
  706. }
  707. }
  708. void ScInterpreter::MEMat(ScMatrix* mM, USHORT n)
  709. {
  710. mM->FillDouble(0.0, 0, 0, n-1, n-1);
  711. for (USHORT i = 0; i < n; i++)
  712. mM->PutDouble(1.0, i, i);
  713. }
  714. void ScInterpreter::MFastMult(ScMatrix* pA, ScMatrix* pB, ScMatrix* pR,
  715. USHORT n, USHORT m, USHORT l)
  716. // Multipliziert n x m Mat a mit m x l Mat b nach Mat r
  717. {
  718. double sum;
  719. for (USHORT i = 0; i < n; i++)
  720. {
  721. for (USHORT j = 0; j < l; j++)
  722. {
  723. sum = 0.0;
  724. for (USHORT k = 0; k < m; k++)
  725. sum += pA->GetDouble(i,k)*pB->GetDouble(k,j);
  726. pR->PutDouble(sum, i, j);
  727. }
  728. }
  729. }
  730. void ScInterpreter::MFastSub(ScMatrix* pA, ScMatrix* pB, ScMatrix* pR,
  731. USHORT n, USHORT m)
  732. // Subtrahiert n x m Mat a - m x l Mat b nach Mat r
  733. {
  734. for (USHORT i = 0; i < n; i++)
  735. {
  736. for (USHORT j = 0; j < m; j++)
  737. pR->PutDouble(pA->GetDouble(i,j) - pB->GetDouble(i,j), i, j);
  738. }
  739. }
  740. void ScInterpreter::MFastTrans(ScMatrix* pA, ScMatrix* pR,
  741. USHORT n, USHORT m)
  742. // Transponiert n x m Mat a nach Mat r
  743. {
  744. for (USHORT i = 0; i < n; i++)
  745. for (USHORT j = 0; j < m; j++)
  746. pR->PutDouble(pA->GetDouble(i, j), j, i);
  747. }
  748. BOOL ScInterpreter::MFastBackSubst(ScMatrix* pA, ScMatrix* pR, USHORT n, BOOL bIsUpper)
  749. // Führt Rückwaertsersetzung der Dreickesmatrix Mat a nach Mat r durch
  750. // 2 Versionen fuer obere (U) oder untere (L- Unit) Dreiecksmatrizen
  751. {
  752. short i, j, k;
  753. double fSum;
  754. if (!bIsUpper) // L-Matrix, immer invertierbar
  755. {
  756. MEMat(pR, n);
  757. for (i = 1; i < (short) n; i++)
  758. {
  759. for (j = 0; j < i; j++)
  760. {
  761. fSum = 0.0;
  762. for (k = 0; k < i; k++)
  763. fSum += pA->GetDouble(i,k) * pR->GetDouble(k,j);
  764. pR->PutDouble(-fSum, i, j);
  765. }
  766. }
  767. }
  768. else // U-Matrix
  769. {
  770. for (i = 0; i < (short) n; i++) // Ist invertierbar?
  771. if (fabs(pA->GetDouble(i,i)) < SCdEpsilon)
  772. return FALSE;
  773. pR->FillDoubleLowerLeft(0.0, n-1); // untere Haelfte
  774. pR->PutDouble(1.0/pA->GetDouble(n-1, n-1), n-1, n-1); // n-1, n-1
  775. for (i = (short) n-2; i >= 0; i--)
  776. {
  777. for (j = (short) n-1; j > i; j--)
  778. {
  779. fSum = 0.0;
  780. for (k = (short) n-1; k > i; k--)
  781. fSum += pA->GetDouble(i, k) * pR->GetDouble(k, j);
  782. pR->PutDouble(-fSum/pA->GetDouble(i, i), i, j);
  783. }
  784. fSum = 0.0; // Hauptdiagonale:
  785. for (k = (short) n-1; k > i; k--)
  786. fSum += pA->GetDouble(i, k) * pR->GetDouble(k, j);
  787. pR->PutDouble((1.0-fSum)/pA->GetDouble(i, i), i, i);
  788. }
  789. }
  790. return TRUE;
  791. }
  792. BOOL ScInterpreter::ScMatLUP(ScMatrix* mA, USHORT m, USHORT p,
  793. ScMatrix* mL, ScMatrix* mU, ScMatrix* mP,
  794. ULONG& rPermutCounter, BOOL& bIsInvertable)
  795. // Returnwert = False <=> Matrixarray voll
  796. // BIsInvertable = False: <= mA hat nicht Rang m
  797. {
  798. USHORT nMatInd1, nMatInd2, nMatInd3, nMatInd4, nMatInd5;
  799. USHORT i, j;
  800. if (m == 1)
  801. {
  802. mL->PutDouble(1.0,0,0);
  803. for (j = 0; j < p; j++)
  804. if (fabs(mA->GetDouble(0, j)) >= SCdEpsilon)
  805. break;
  806. if (j == p)
  807. {
  808. bIsInvertable = FALSE;
  809. return TRUE;
  810. }
  811. MEMat(mP, p);
  812. if (j > 0 && j < p)
  813. {
  814. mP->PutDouble(0.0, 0, 0);
  815. mP->PutDouble(1.0, j, 0);
  816. mP->PutDouble(0.0, j, j);
  817. mP->PutDouble(1.0, 0, j);
  818. rPermutCounter++;
  819. }
  820. MFastMult(mA, mP, mU, m, p, p);
  821. }
  822. else
  823. {
  824. USHORT md2 = m/2;
  825. ScMatrix* mB = GetNewMat(md2, p, nMatInd1);
  826. ScMatrix* mC = GetNewMat(md2, p, nMatInd2);
  827. ScMatrix* mL1 = GetNewMat(md2, md2, nMatInd3);
  828. ScMatrix* mU1 = GetNewMat(md2, p, nMatInd4);
  829. ScMatrix* mP1 = GetNewMat(p, p, nMatInd5);
  830. if (!mB || !mC || !mL1 || !mU1 || !mP1 )
  831. return FALSE;
  832. for (i = 0; i < md2; i++)
  833. {
  834. for (j = 0; j < p; j++)
  835. {
  836. mB->PutDouble(mA->GetDouble(i, j), i, j);
  837. mC->PutDouble(mA->GetDouble(md2+i,j), i, j);
  838. }
  839. }
  840. if (!ScMatLUP(mB, md2, p, mL1, mU1, mP1, rPermutCounter, bIsInvertable))
  841. return FALSE;
  842. if (!bIsInvertable)
  843. {
  844. ResetNewMat(nMatInd5);
  845. ResetNewMat(nMatInd4);
  846. ResetNewMat(nMatInd3);
  847. ResetNewMat(nMatInd2);
  848. ResetNewMat(nMatInd1);
  849. delete mP1;
  850. delete mU1;
  851. delete mL1;
  852. delete mC;
  853. delete mB;
  854. return TRUE;
  855. }
  856. USHORT nMatInd6, nMatInd7, nMatInd8, nMatInd9, nMatInd10;
  857. USHORT nMatInd11, nMatInd12, nMatInd13;
  858. ScMatrix* mE = GetNewMat(md2, md2, nMatInd6);
  859. ScMatrix* mF = GetNewMat(md2, md2, nMatInd7);
  860. ScMatrix* mEInv = GetNewMat(md2, md2, nMatInd8);
  861. ScMatrix* mG = GetNewMat(md2, p, nMatInd9);
  862. ScMatrix* mGs = GetNewMat(md2, p - md2, nMatInd10);
  863. ScMatrix* mU2 = GetNewMat(md2, p - md2, nMatInd11);
  864. ScMatrix* mP2 = GetNewMat(p - md2, p - md2, nMatInd12);
  865. if (!mP2 || !mU2 || !mGs|| !mG || !mEInv || !mF || !mE)
  866. return FALSE;
  867. MFastTrans(mP1, mP, p, p); // mP = mP1 hoch -1
  868. ScMatrix* mD = mB; // mB wird nicht mehr gebraucht
  869. MFastMult(mC, mP, mD, md2, p, p);
  870. for (i = 0; i < md2; i++)
  871. {
  872. for (j = 0; j < md2; j++)
  873. {
  874. mE->PutDouble(mU1->GetDouble(i, j), i, j);
  875. mF->PutDouble(mD->GetDouble(i, j), i, j);
  876. }
  877. }
  878. BOOL bEInvok = MFastBackSubst(mE, mEInv, md2, TRUE); // MeInv = E hoch -1
  879. if (!bEInvok)
  880. {
  881. bIsInvertable = FALSE;
  882. ResetNewMat(nMatInd12);
  883. ResetNewMat(nMatInd11);
  884. ResetNewMat(nMatInd10);
  885. ResetNewMat(nMatInd9);
  886. ResetNewMat(nMatInd8);
  887. ResetNewMat(nMatInd7);
  888. ResetNewMat(nMatInd6);
  889. ResetNewMat(nMatInd5);
  890. ResetNewMat(nMatInd4);
  891. ResetNewMat(nMatInd3);
  892. ResetNewMat(nMatInd2);
  893. ResetNewMat(nMatInd1);
  894. delete mP2;
  895. delete mU2;
  896. delete mGs;
  897. delete mG;
  898. delete mEInv;
  899. delete mF;
  900. delete mE;
  901. delete mP1;
  902. delete mU1;
  903. delete mL1;
  904. delete mC;
  905. delete mB;
  906. return TRUE;
  907. }
  908. ScMatrix* mFEInv = mE; // mE wird nicht mehr gebraucht.
  909. MFastMult(mF, mEInv, mFEInv, md2, md2, md2);
  910. ScMatrix* mFEInvU1 = mC; // mC wird nicht mehr gebraucht
  911. MFastMult(mFEInv, mU1, mFEInvU1, md2, md2, p);
  912. MFastSub(mD, mFEInvU1, mG, md2, p);
  913. for (i = 0; i < md2; i++)
  914. {
  915. for (j = 0; j < p-md2; j++)
  916. mGs->PutDouble(mG->GetDouble(i, md2+j), i, j);
  917. }
  918. ScMatrix* mL2 = mF; // mF wird nicht mehr gebraucht
  919. if (!ScMatLUP(mGs, md2, p - md2, mL2, mU2, mP2, rPermutCounter, bIsInvertable))
  920. return FALSE;
  921. if (!bIsInvertable)
  922. {
  923. ResetNewMat(nMatInd12);
  924. ResetNewMat(nMatInd11);
  925. ResetNewMat(nMatInd10);
  926. ResetNewMat(nMatInd9);
  927. ResetNewMat(nMatInd8);
  928. ResetNewMat(nMatInd7);
  929. ResetNewMat(nMatInd6);
  930. ResetNewMat(nMatInd5);
  931. ResetNewMat(nMatInd4);
  932. ResetNewMat(nMatInd3);
  933. ResetNewMat(nMatInd2);
  934. ResetNewMat(nMatInd1);
  935. delete mP2;
  936. delete mU2;
  937. delete mGs;
  938. delete mG;
  939. delete mEInv;
  940. delete mF;
  941. delete mE;
  942. delete mP1;
  943. delete mU1;
  944. delete mL1;
  945. delete mC;
  946. delete mB;
  947. return TRUE;
  948. }
  949. ScMatrix* mP3 = GetNewMat(p, p, nMatInd13);
  950. if (!mP3)
  951. return FALSE;
  952. MEMat(mP3, p);
  953. for (i = md2; i < p; i++)
  954. {
  955. for (j = md2; j < p; j++)
  956. mP3->PutDouble(mP2->GetDouble(i-md2, j-md2), i, j);
  957. }
  958. MFastMult(mP3, mP1, mP, p, p, p); // Ergebnis P !!
  959. ScMatrix* mP3Inv = mP1; // mP1 wird nicht mehr gebraucht;
  960. MFastTrans(mP3, mP3Inv, p, p);
  961. ScMatrix* mH = mD; // mD wird nicht mehr gebraucht
  962. MFastMult(mU1, mP3Inv, mH, md2, p, p);
  963. MEMat(mL, m); // Ergebnis L :
  964. for (i = 0; i < md2; i++)
  965. {
  966. for (j = 0; j < i; j++)
  967. mL->PutDouble(mL1->GetDouble(i, j), i, j);
  968. }
  969. for (i = md2; i < m; i++)
  970. for (j = md2; j < i; j++)
  971. mL->PutDouble(mL2->GetDouble(i-md2, j-md2), i, j);
  972. for (i = md2; i < m; i++)
  973. for (j = 0; j < md2; j++)
  974. mL->PutDouble(mFEInv->GetDouble(i-md2, j), i, j);
  975. // Ergebnis U:
  976. mU->FillDoubleLowerLeft(0.0, m-1);
  977. for (i = 0; i < md2; i++)
  978. for (j = i; j < p; j++)
  979. mU->PutDouble(mH->GetDouble(i, j), i, j);
  980. for (i = md2; i < m; i++)
  981. for (j = i; j < p; j++)
  982. mU->PutDouble(mU2->GetDouble(i - md2, j - md2), i, j);
  983. ResetNewMat(nMatInd13); // alle wieder freigeben;
  984. ResetNewMat(nMatInd12);
  985. ResetNewMat(nMatInd11);
  986. ResetNewMat(nMatInd10);
  987. ResetNewMat(nMatInd9);
  988. ResetNewMat(nMatInd8);
  989. ResetNewMat(nMatInd7);
  990. ResetNewMat(nMatInd6);
  991. ResetNewMat(nMatInd5);
  992. ResetNewMat(nMatInd4);
  993. ResetNewMat(nMatInd3);
  994. ResetNewMat(nMatInd2);
  995. ResetNewMat(nMatInd1);
  996. delete mP3;
  997. delete mP2;
  998. delete mU2;
  999. delete mGs;
  1000. delete mG;
  1001. delete mEInv;
  1002. delete mF;
  1003. delete mE;
  1004. delete mP1;
  1005. delete mU1;
  1006. delete mL1;
  1007. delete mC;
  1008. delete mB;
  1009. }
  1010. return TRUE;
  1011. }
  1012. void ScInterpreter::ScMatDet()
  1013. {
  1014. if ( MustHaveParamCount( GetByte(), 1 ) )
  1015. {
  1016. USHORT nMatInd;
  1017. ScMatrix* pMat = GetMatrix(nMatInd);
  1018. if (!pMat)
  1019. {
  1020. SetIllegalParameter();
  1021. return;
  1022. }
  1023. if ( !pMat->IsNumeric() )
  1024. {
  1025. SetNoValue();
  1026. return;
  1027. }
  1028. USHORT nC, nR;
  1029. pMat->GetDimensions(nC, nR);
  1030. if ( nC != nR || nC == 0 || (ULONG) nC * nC > ScMatrix::GetElementsMax() )
  1031. SetIllegalParameter();
  1032. else
  1033. {
  1034. double fVal = log((double)nC) / log(2.0);
  1035. if (fVal - floor(fVal) != 0.0)
  1036. fVal = floor(fVal) + 1.0;
  1037. USHORT nDim = (USHORT) pow(2.0, fVal);
  1038. USHORT nMatInd1, nMatInd2, nMatInd3;
  1039. USHORT nMatInd4 = MAX_ANZ_MAT;
  1040. ScMatrix* pU = GetNewMat(nDim, nDim, nMatInd1);
  1041. ScMatrix* pL = GetNewMat(nDim, nDim, nMatInd2);
  1042. ScMatrix* pP = GetNewMat(nDim, nDim, nMatInd3);
  1043. ScMatrix* pA;
  1044. if (nC == nDim)
  1045. pA = pMat;
  1046. else
  1047. {
  1048. pA = GetNewMat(nDim, nDim, nMatInd4);
  1049. MEMat(pA, nDim);
  1050. for (USHORT i = 0; i < nC; i++)
  1051. for (USHORT j = 0; j < nC; j++)
  1052. {
  1053. pA->PutDouble(pMat->GetDouble(i, j), i, j);
  1054. }
  1055. }
  1056. ULONG nPermutCounter = 0;
  1057. BOOL bIsInvertable = TRUE;
  1058. BOOL bOk = ScMatLUP(pA, nDim, nDim, pL, pU, pP,
  1059. nPermutCounter, bIsInvertable);
  1060. ResetNewMat(nMatInd4);
  1061. ResetNewMat(nMatInd3);
  1062. ResetNewMat(nMatInd2);
  1063. if (nC != nDim)
  1064. delete pA;
  1065. delete pP;
  1066. delete pL;
  1067. if (bOk)
  1068. {
  1069. if (!bIsInvertable)
  1070. PushInt(0);
  1071. else
  1072. {
  1073. double fDet = 1.0;
  1074. for (USHORT i = 0; i < nC; i++)
  1075. fDet *= pU->GetDouble(i, i);
  1076. if (nPermutCounter % 2 != 0)
  1077. fDet *= -1.0;
  1078. PushDouble(fDet);
  1079. }
  1080. }
  1081. else
  1082. {
  1083. SetError(errCodeOverflow);
  1084. PushInt(0);
  1085. }
  1086. ResetNewMat(nMatInd1);
  1087. delete pU;
  1088. }
  1089. }
  1090. }
  1091. void ScInterpreter::ScMatInv()
  1092. {
  1093. if ( MustHaveParamCount( GetByte(), 1 ) )
  1094. {
  1095. USHORT nMatInd;
  1096. ScMatrix* pMat = GetMatrix(nMatInd);
  1097. if (!pMat)
  1098. {
  1099. SetIllegalParameter();
  1100. return;
  1101. }
  1102. if ( !pMat->IsNumeric() )
  1103. {
  1104. SetNoValue();
  1105. return;
  1106. }
  1107. USHORT nC, nR;
  1108. pMat->GetDimensions(nC, nR);
  1109. if ( nC != nR || nC == 0 || (ULONG) nC * nC > ScMatrix::GetElementsMax() )
  1110. SetIllegalParameter();
  1111. else
  1112. {
  1113. double fVal = log((double)nC) / log(2.0);
  1114. if (fVal - floor(fVal) != 0.0)
  1115. fVal = floor(fVal) + 1.0;
  1116. USHORT nDim = (USHORT) pow(2.0, fVal);
  1117. USHORT nMatInd1, nMatInd2, nMatInd3;
  1118. USHORT nMatInd4 = MAX_ANZ_MAT;
  1119. ScMatrix* pU = GetNewMat(nDim, nDim, nMatInd1);
  1120. ScMatrix* pL = GetNewMat(nDim, nDim, nMatInd2);
  1121. ScMatrix* pP = GetNewMat(nDim, nDim, nMatInd3);
  1122. ScMatrix* pA;
  1123. if (nC == nDim)
  1124. pA = pMat;
  1125. else
  1126. {
  1127. pA = GetNewMat(nDim, nDim, nMatInd4);
  1128. MEMat(pA, nDim);
  1129. for (USHORT i = 0; i < nC; i++)
  1130. for (USHORT j = 0; j < nC; j++)
  1131. {
  1132. pA->PutDouble(pMat->GetDouble(i, j), i, j);
  1133. }
  1134. }
  1135. ULONG nPermutCounter = 0;
  1136. BOOL bIsInvertable = TRUE;
  1137. BOOL bOk = ScMatLUP(pA, nDim, nDim, pL, pU, pP,
  1138. nPermutCounter, bIsInvertable);
  1139. if (bOk)
  1140. {
  1141. if (!bIsInvertable)
  1142. SetNoValue();
  1143. else
  1144. {
  1145. USHORT nMatInd5;
  1146. ScMatrix* pUInv = GetNewMat(nDim, nDim, nMatInd5);
  1147. if (!pUInv)
  1148. PushInt(0);
  1149. else
  1150. {
  1151. bOk = MFastBackSubst(pU, pUInv, nDim, TRUE);
  1152. if (!bOk)
  1153. SetNoValue();
  1154. else
  1155. {
  1156. ScMatrix* pPInv = pU;
  1157. MFastTrans(pP, pPInv, nDim, nDim);
  1158. ScMatrix* pPInvUInv = pP;
  1159. MFastMult(pPInv, pUInv, pPInvUInv, nDim, nDim, nDim);
  1160. ScMatrix* pLInv = pPInv;
  1161. MFastBackSubst(pL, pLInv, nDim, FALSE);
  1162. if (nDim == nC)
  1163. MFastMult(pPInvUInv, pLInv, pMat, nDim, nDim, nDim);
  1164. else
  1165. {
  1166. MFastMult(pPInvUInv, pLInv, pL, nDim, nDim, nDim);
  1167. for (USHORT i = 0; i < nC; i++)
  1168. for (USHORT j = 0; j < nC; j++)
  1169. pMat->PutDouble(pL->GetDouble(i, j), i, j);
  1170. }
  1171. PushMatrix(pMat);
  1172. if (nMatInd != MAX_ANZ_MAT) // vorher neue Matrix
  1173. nRetMat = nMatInd; // sonst nRetMat wie vorher
  1174. ResetNewMat(nMatInd5);
  1175. delete pUInv;
  1176. }
  1177. }
  1178. }
  1179. }
  1180. else
  1181. {
  1182. SetError(errCodeOverflow);
  1183. PushInt(0);
  1184. }
  1185. ResetNewMat(nMatInd4);
  1186. ResetNewMat(nMatInd3);
  1187. ResetNewMat(nMatInd2);
  1188. ResetNewMat(nMatInd1);
  1189. if (nC != nDim)
  1190. delete pA;
  1191. delete pP;
  1192. delete pL;
  1193. delete pU;
  1194. }
  1195. }
  1196. }
  1197. void ScInterpreter::ScMatMult()
  1198. {
  1199. if ( MustHaveParamCount( GetByte(), 2 ) )
  1200. {
  1201. USHORT nMatInd1, nMatInd2;
  1202. ScMatrix* pMat2 = GetMatrix(nMatInd2);
  1203. ScMatrix* pMat1 = GetMatrix(nMatInd1);
  1204. ScMatrix* pRMat;
  1205. if (pMat1 && pMat2)
  1206. {
  1207. if ( pMat1->IsNumeric() && pMat2->IsNumeric() )
  1208. {
  1209. USHORT nC1, nR1, nC2, nR2;
  1210. pMat1->GetDimensions(nC1, nR1);
  1211. pMat2->GetDimensions(nC2, nR2);
  1212. if (nC1 != nR2)
  1213. SetIllegalParameter();
  1214. else
  1215. {
  1216. USHORT nMatInd;
  1217. pRMat = GetNewMat(nC2, nR1, nMatInd); // Matrixabsicherung
  1218. if (pRMat)
  1219. {
  1220. double sum;
  1221. for (USHORT i = 0; i < nR1; i++)
  1222. {
  1223. for (USHORT j = 0; j < nC2; j++)
  1224. {
  1225. sum = 0.0;
  1226. for (USHORT k = 0; k < nC1; k++)
  1227. {
  1228. sum += pMat1->GetDouble(k,i)*pMat2->GetDouble(j,k);
  1229. }
  1230. pRMat->PutDouble(sum, j, i);
  1231. }
  1232. }
  1233. PushMatrix(pRMat);
  1234. nRetMat = nMatInd;
  1235. }
  1236. else
  1237. SetNoValue();
  1238. }
  1239. }
  1240. else
  1241. SetNoValue();
  1242. }
  1243. else
  1244. SetIllegalParameter();
  1245. }
  1246. }
  1247. void ScInterpreter::ScMatTrans()
  1248. {
  1249. if ( MustHaveParamCount( GetByte(), 1 ) )
  1250. {
  1251. USHORT nMatInd, nMatInd1;
  1252. ScMatrix* pMat = GetMatrix(nMatInd);
  1253. ScMatrix* pRMat;
  1254. if (pMat)
  1255. {
  1256. USHORT nC, nR;
  1257. pMat->GetDimensions(nC, nR);
  1258. pRMat = GetNewMat(nR, nC, nMatInd1);
  1259. pMat->MatTrans(*pRMat);
  1260. PushMatrix(pRMat);
  1261. nRetMat = nMatInd1;
  1262. }
  1263. else
  1264. SetIllegalParameter();
  1265. }
  1266. }
  1267. /*N*/ ScMatrix* ScInterpreter::MatAdd(ScMatrix* pMat1, ScMatrix* pMat2)
  1268. /*N*/ {
  1269. /*N*/ USHORT nC1, nR1, nC2, nR2, nMinC, nMinR, i, j;
  1270. /*N*/ pMat1->GetDimensions(nC1, nR1);
  1271. /*N*/ pMat2->GetDimensions(nC2, nR2);
  1272. /*N*/ if (nC1 < nC2)
  1273. /*N*/ nMinC = nC1;
  1274. /*N*/ else
  1275. /*N*/ nMinC = nC2;
  1276. /*N*/ if (nR1 < nR2)
  1277. /*N*/ nMinR = nR1;
  1278. /*N*/ else
  1279. /*N*/ nMinR = nR2;
  1280. /*N*/ USHORT nMatInd;
  1281. /*N*/ ScMatrix* pResMat = GetNewMat(nMinC, nMinR, nMatInd);
  1282. /*N*/ if (pResMat)
  1283. /*N*/ {
  1284. /*N*/ for (i = 0; i < nMinC; i++)
  1285. /*N*/ {
  1286. /*N*/ for (j = 0; j < nMinR; j++)
  1287. /*N*/ {
  1288. /*N*/ if (pMat1->IsValueOrEmpty(i,j) && pMat2->IsValueOrEmpty(i,j))
  1289. /*N*/ pResMat->PutDouble( ::rtl::math::approxAdd( pMat1->GetDouble(i,j),
  1290. /*N*/ pMat2->GetDouble(i,j)), i, j);
  1291. /*N*/ else
  1292. /*?*/ pResMat->PutString(ScGlobal::GetRscString(
  1293. /*N*/ STR_NO_VALUE), i, j);
  1294. /*N*/ }
  1295. /*N*/ }
  1296. /*N*/ nRetMat = nMatInd;
  1297. /*N*/ }
  1298. /*N*/ return pResMat;
  1299. /*N*/ }
  1300. ScMatrix* ScInterpreter::MatSub(ScMatrix* pMat1, ScMatrix* pMat2)
  1301. {
  1302. USHORT nC1, nR1, nC2, nR2, nMinC, nMinR, i, j;
  1303. pMat1->GetDimensions(nC1, nR1);
  1304. pMat2->GetDimensions(nC2, nR2);
  1305. if (nC1 < nC2)
  1306. nMinC = nC1;
  1307. else
  1308. nMinC = nC2;
  1309. if (nR1 < nR2)
  1310. nMinR = nR1;
  1311. else
  1312. nMinR = nR2;
  1313. USHORT nMatInd;
  1314. ScMatrix* pResMat = GetNewMat(nMinC, nMinR, nMatInd);
  1315. if (pResMat)
  1316. {
  1317. for (i = 0; i < nMinC; i++)
  1318. {
  1319. for (j = 0; j < nMinR; j++)
  1320. {
  1321. if (pMat1->IsValueOrEmpty(i,j) && pMat2->IsValueOrEmpty(i,j))
  1322. pResMat->PutDouble( ::rtl::math::approxSub( pMat1->GetDouble(i,j),
  1323. pMat2->GetDouble(i,j)), i, j);
  1324. else
  1325. pResMat->PutString(ScGlobal::GetRscString(
  1326. STR_NO_VALUE), i, j);
  1327. }
  1328. }
  1329. nRetMat = nMatInd;
  1330. }
  1331. return pResMat;
  1332. }
  1333. /*N*/ ScMatrix* ScInterpreter::MatMul(ScMatrix* pMat1, ScMatrix* pMat2)
  1334. /*N*/ {
  1335. /*N*/ USHORT nC1, nR1, nC2, nR2, nMinC, nMinR, i, j;
  1336. /*N*/ pMat1->GetDimensions(nC1, nR1);
  1337. /*N*/ pMat2->GetDimensions(nC2, nR2);
  1338. /*N*/ if (nC1 < nC2)
  1339. /*N*/ nMinC = nC1;
  1340. /*N*/ else
  1341. /*N*/ nMinC = nC2;
  1342. /*N*/ if (nR1 < nR2)
  1343. /*N*/ nMinR = nR1;
  1344. /*N*/ else
  1345. /*N*/ nMinR = nR2;
  1346. /*N*/ USHORT nMatInd;
  1347. /*N*/ ScMatrix* pResMat = GetNewMat(nMinC, nMinR, nMatInd);
  1348. /*N*/ if (pResMat)
  1349. /*N*/ {
  1350. /*N*/ for (i = 0; i < nMinC; i++)
  1351. /*N*/ {
  1352. /*N*/ for (j = 0; j < nMinR; j++)
  1353. /*N*/ {
  1354. /*N*/ if (pMat1->IsValueOrEmpty(i,j) && pMat2->IsValueOrEmpty(i,j))
  1355. /*N*/ pResMat->PutDouble(pMat1->GetDouble(i,j) *
  1356. /*N*/ pMat2->GetDouble(i,j), i, j);
  1357. /*N*/ else
  1358. /*?*/ pResMat->PutString(ScGlobal::GetRscString(
  1359. /*N*/ STR_NO_VALUE), i, j);
  1360. /*N*/ }
  1361. /*N*/ }
  1362. /*N*/ nRetMat = nMatInd;
  1363. /*N*/ }
  1364. /*N*/ return pResMat;
  1365. /*N*/ }
  1366. ScMatrix* ScInterpreter::MatDiv(ScMatrix* pMat1, ScMatrix* pMat2)
  1367. {
  1368. USHORT nC1, nR1, nC2, nR2, nMinC, nMinR, i, j;
  1369. pMat1->GetDimensions(nC1, nR1);
  1370. pMat2->GetDimensions(nC2, nR2);
  1371. if (nC1 < nC2)
  1372. nMinC = nC1;
  1373. else
  1374. nMinC = nC2;
  1375. if (nR1 < nR2)
  1376. nMinR = nR1;
  1377. else
  1378. nMinR = nR2;
  1379. USHORT nMatInd;
  1380. ScMatrix* pResMat = GetNewMat(nMinC, nMinR, nMatInd);
  1381. if (pResMat)
  1382. {
  1383. for (i = 0; i < nMinC; i++)
  1384. {
  1385. for (j = 0; j < nMinR; j++)
  1386. {
  1387. if (pMat1->IsValueOrEmpty(i,j) && pMat2->IsValueOrEmpty(i,j))
  1388. pResMat->PutDouble(pMat1->GetDouble(i,j) /
  1389. pMat2->GetDouble(i,j), i, j);
  1390. else
  1391. pResMat->PutString(ScGlobal::GetRscString(
  1392. STR_NO_VALUE), i, j);
  1393. }
  1394. }
  1395. nRetMat = nMatInd;
  1396. }
  1397. return pResMat;
  1398. }
  1399. ScMatrix* ScInterpreter::MatPow(ScMatrix* pMat1, ScMatrix* pMat2)
  1400. {
  1401. USHORT nC1, nR1, nC2, nR2, nMinC, nMinR, i, j;
  1402. pMat1->GetDimensions(nC1, nR1);
  1403. pMat2->GetDimensions(nC2, nR2);
  1404. if (nC1 < nC2)
  1405. nMinC = nC1;
  1406. else
  1407. nMinC = nC2;
  1408. if (nR1 < nR2)
  1409. nMinR = nR1;
  1410. else
  1411. nMinR = nR2;
  1412. USHORT nMatInd;
  1413. ScMatrix* pResMat = GetNewMat(nMinC, nMinR, nMatInd);
  1414. if (pResMat)
  1415. {
  1416. for (i = 0; i < nMinC; i++)
  1417. {
  1418. for (j = 0; j < nMinR; j++)
  1419. {
  1420. if (pMat1->IsValueOrEmpty(i,j) && pMat2->IsValueOrEmpty(i,j))
  1421. pResMat->PutDouble(pow(pMat1->GetDouble(i,j),
  1422. pMat2->GetDouble(i,j)), i, j);
  1423. else
  1424. pResMat->PutString(ScGlobal::GetRscString(
  1425. STR_NO_VALUE), i, j);
  1426. }
  1427. }
  1428. nRetMat = nMatInd;
  1429. }
  1430. return pResMat;
  1431. }
  1432. ScMatrix* ScInterpreter::MatConcat(ScMatrix* pMat1, ScMatrix* pMat2)
  1433. {
  1434. USHORT nC1, nR1, nC2, nR2, nMinC, nMinR, i, j;
  1435. pMat1->GetDimensions(nC1, nR1);
  1436. pMat2->GetDimensions(nC2, nR2);
  1437. if (nC1 < nC2)
  1438. nMinC = nC1;
  1439. else
  1440. nMinC = nC2;
  1441. if (nR1 < nR2)
  1442. nMinR = nR1;
  1443. else
  1444. nMinR = nR2;
  1445. USHORT nMatInd;
  1446. ScMatrix* pResMat = GetNewMat(nMinC, nMinR, nMatInd);
  1447. if (pResMat)
  1448. {
  1449. for (i = 0; i < nMinC; i++)
  1450. {
  1451. for (j = 0; j < nMinR; j++)
  1452. {
  1453. if (pMat1->IsString(i,j) && pMat2->IsString(i,j))
  1454. {
  1455. String aTmp( pMat1->GetString(i,j) );
  1456. aTmp += pMat2->GetString(i,j);
  1457. pResMat->PutString( aTmp , i, j);
  1458. }
  1459. else
  1460. pResMat->PutString(ScGlobal::GetRscString(
  1461. STR_NO_VALUE), i, j);
  1462. //! TODO: Xcl does concatenate value strings
  1463. }
  1464. }
  1465. nRetMat = nMatInd;
  1466. }
  1467. return pResMat;
  1468. }
  1469. // fuer DATE, TIME, DATETIME
  1470. /*N*/ void lcl_GetDiffDateTimeFmtType( short& nFuncFmt, short nFmt1, short nFmt2 )
  1471. /*N*/ {
  1472. /*N*/ if ( nFmt1 != NUMBERFORMAT_UNDEFINED || nFmt2 != NUMBERFORMAT_UNDEFINED )
  1473. /*N*/ {
  1474. /*N*/ if ( nFmt1 == nFmt2 )
  1475. /*N*/ {
  1476. /*N*/ if ( nFmt1 == NUMBERFORMAT_TIME || nFmt1 == NUMBERFORMAT_DATETIME )
  1477. /*N*/ nFuncFmt = NUMBERFORMAT_TIME; // Zeiten ergeben Zeit
  1478. /*N*/ // else: nichts besonderes, Zahl (Datum - Datum := Tage)
  1479. /*N*/ }
  1480. /*N*/ else if ( nFmt1 == NUMBERFORMAT_UNDEFINED )
  1481. /*N*/ nFuncFmt = nFmt2; // z.B. Datum + Tage := Datum
  1482. /*N*/ else if ( nFmt2 == NUMBERFORMAT_UNDEFINED )
  1483. /*N*/ nFuncFmt = nFmt1;
  1484. /*N*/ else
  1485. /*N*/ {
  1486. /*N*/ if ( nFmt1 == NUMBERFORMAT_DATE || nFmt2 == NUMBERFORMAT_DATE ||
  1487. /*N*/ nFmt1 == NUMBERFORMAT_DATETIME || nFmt2 == NUMBERFORMAT_DATETIME )
  1488. /*N*/ {
  1489. /*N*/ if ( nFmt1 == NUMBERFORMAT_TIME || nFmt2 == NUMBERFORMAT_TIME )
  1490. /*N*/ nFuncFmt = NUMBERFORMAT_DATETIME; // Datum + Zeit
  1491. /*N*/ }
  1492. /*N*/ }
  1493. /*N*/ }
  1494. /*N*/ }
  1495. /*N*/ void ScInterpreter::ScAdd()
  1496. /*N*/ {
  1497. /*N*/ ScMatrix* pMat1 = NULL;
  1498. /*N*/ ScMatrix* pMat2 = NULL;
  1499. /*N*/ double fVal1 = 0.0, fVal2 = 0.0;
  1500. /*N*/ USHORT nMatInd1, nMatInd2;
  1501. /*N*/ short nFmt1, nFmt2;
  1502. /*N*/ nFmt1 = nFmt2 = NUMBERFORMAT_UNDEFINED;
  1503. /*N*/ short nFmtCurrencyType = nCurFmtType;
  1504. /*N*/ ULONG nFmtCurrencyIndex = nCurFmtIndex;
  1505. /*N*/ short nFmtPercentType = nCurFmtType;
  1506. /*N*/ MatrixDoubleRefToMatrix();
  1507. /*N*/ if ( GetStackType() == svMatrix )
  1508. /*N*/ pMat2 = GetMatrix(nMatInd2);
  1509. /*N*/ else
  1510. /*N*/ {
  1511. /*N*/ fVal2 = GetDouble();
  1512. /*N*/ switch ( nCurFmtType )
  1513. /*N*/ {
  1514. /*N*/ case NUMBERFORMAT_DATE :
  1515. /*N*/ case NUMBERFORMAT_TIME :
  1516. /*N*/ case NUMBERFORMAT_DATETIME :
  1517. /*N*/ nFmt2 = nCurFmtType;
  1518. /*N*/ break;
  1519. /*N*/ case NUMBERFORMAT_CURRENCY :
  1520. /*N*/ nFmtCurrencyType = nCurFmtType;
  1521. /*N*/ nFmtCurrencyIndex = nCurFmtIndex;
  1522. /*N*/ break;
  1523. /*N*/ case NUMBERFORMAT_PERCENT :
  1524. /*N*/ nFmtPercentType = NUMBERFORMAT_PERCENT;
  1525. /*N*/ break;
  1526. /*N*/ }
  1527. /*N*/ }
  1528. /*N*/ MatrixDoubleRefToMatrix();
  1529. /*N*/ if ( GetStackType() == svMatrix )
  1530. /*N*/ pMat1 = GetMatrix(nMatInd1);
  1531. /*N*/ else
  1532. /*N*/ {
  1533. /*N*/ fVal1 = GetDouble();
  1534. /*N*/ switch ( nCurFmtType )
  1535. /*N*/ {
  1536. /*N*/ case NUMBERFORMAT_DATE :
  1537. /*N*/ case NUMBERFORMAT_TIME :
  1538. /*N*/ case NUMBERFORMAT_DATETIME :
  1539. /*N*/ nFmt1 = nCurFmtType;
  1540. /*N*/ break;
  1541. /*N*/ case NUMBERFORMAT_CURRENCY :
  1542. /*N*/ nFmtCurrencyType = nCurFmtType;
  1543. /*N*/ nFmtCurrencyIndex = nCurFmtIndex;
  1544. /*N*/ break;
  1545. /*N*/ case NUMBERFORMAT_PERCENT :
  1546. /*N*/ nFmtPercentType = NUMBERFORMAT_PERCENT;
  1547. /*N*/ break;
  1548. /*N*/ }
  1549. /*N*/ }
  1550. /*N*/ if (pMat1 && pMat2)
  1551. /*N*/ {
  1552. /*N*/ ScMatrix* pResMat = MatAdd(pMat1, pMat2);
  1553. /*N*/ if (!pResMat)
  1554. /*?*/ SetNoValue();
  1555. /*N*/ else
  1556. /*N*/ PushMatrix(pResMat);
  1557. /*N*/ }
  1558. /*N*/ else if (pMat1 || pMat2)
  1559. /*N*/ {
  1560. /*?*/ double fVal;
  1561. /*?*/ ScMatrix* pMat = pMat1;
  1562. /*?*/ if (!pMat)
  1563. /*?*/ {
  1564. /*?*/ fVal = fVal1;
  1565. /*?*/ pMat = pMat2;
  1566. /*?*/ }
  1567. /*?*/ else
  1568. /*?*/ fVal = fVal2;
  1569. /*?*/ USHORT nC, nR;
  1570. /*?*/ pMat->GetDimensions(nC, nR);
  1571. /*?*/ USHORT nMatInd;
  1572. /*?*/ ScMatrix* pResMat = GetNewMat(nC, nR, nMatInd);
  1573. /*?*/ if (pResMat)
  1574. /*?*/ {
  1575. /*?*/ ULONG nCount = (ULONG) nC * nR;
  1576. /*?*/ for ( ULONG i = 0; i < nCount; i++ )
  1577. /*?*/ {
  1578. /*?*/ if (pMat->IsValue(i))
  1579. /*?*/ pResMat->PutDouble( ::rtl::math::approxAdd( pMat->GetDouble(i), fVal), i);
  1580. /*?*/ else
  1581. /*?*/ pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  1582. /*?*/ }
  1583. /*?*/ PushMatrix(pResMat);
  1584. /*?*/ nRetMat = nMatInd;
  1585. /*?*/ }
  1586. /*?*/ else
  1587. /*?*/ SetNoValue();
  1588. /*N*/ }
  1589. /*N*/ else
  1590. /*N*/ PushDouble( ::rtl::math::approxAdd( fVal1, fVal2 ) );
  1591. /*N*/ if ( nFmtCurrencyType == NUMBERFORMAT_CURRENCY )
  1592. /*N*/ {
  1593. /*N*/ nFuncFmtType = nFmtCurrencyType;
  1594. /*N*/ nFuncFmtIndex = nFmtCurrencyIndex;
  1595. /*N*/ }
  1596. /*N*/ else
  1597. /*N*/ {
  1598. /*N*/ lcl_GetDiffDateTimeFmtType( nFuncFmtType, nFmt1, nFmt2 );
  1599. /*N*/ if ( nFmtPercentType == NUMBERFORMAT_PERCENT && nFuncFmtType == NUMBERFORMAT_NUMBER )
  1600. /*N*/ nFuncFmtType = NUMBERFORMAT_PERCENT;
  1601. /*N*/ }
  1602. /*N*/ }
  1603. /*N*/ void ScInterpreter::ScAmpersand()
  1604. /*N*/ {
  1605. /*N*/ ScMatrix* pMat1 = NULL;
  1606. /*N*/ ScMatrix* pMat2 = NULL;
  1607. /*N*/ String sStr1, sStr2;
  1608. /*N*/ USHORT nMatInd1, nMatInd2;
  1609. /*N*/ MatrixDoubleRefToMatrix();
  1610. /*N*/ if ( GetStackType() == svMatrix )
  1611. /*?*/ pMat2 = GetMatrix(nMatInd2);
  1612. /*N*/ else
  1613. /*N*/ sStr2 = GetString();
  1614. /*N*/ MatrixDoubleRefToMatrix();
  1615. /*N*/ if ( GetStackType() == svMatrix )
  1616. /*?*/ pMat1 = GetMatrix(nMatInd1);
  1617. /*N*/ else
  1618. /*N*/ sStr1 = GetString();
  1619. /*N*/ if (pMat1 && pMat2)
  1620. /*N*/ {
  1621. /*?*/ ScMatrix* pResMat = MatConcat(pMat1, pMat2);
  1622. /*?*/ if (!pResMat)
  1623. /*?*/ SetNoValue();
  1624. /*?*/ else
  1625. /*?*/ PushMatrix(pResMat);
  1626. /*N*/ }
  1627. /*N*/ else if (pMat1 || pMat2)
  1628. /*N*/ {
  1629. /*?*/ String sStr;
  1630. /*?*/ BOOL bFlag;
  1631. /*?*/ ScMatrix* pMat = pMat1;
  1632. /*?*/ if (!pMat)
  1633. /*?*/ {
  1634. /*?*/ sStr = sStr1;
  1635. /*?*/ pMat = pMat2;
  1636. /*?*/ bFlag = TRUE; // double - Matrix
  1637. /*?*/ }
  1638. /*?*/ else
  1639. /*?*/ {
  1640. /*?*/ sStr = sStr2;
  1641. /*?*/ bFlag = FALSE; // Matrix - double
  1642. /*?*/ }
  1643. /*?*/ USHORT nC, nR;
  1644. /*?*/ pMat->GetDimensions(nC, nR);
  1645. /*?*/ USHORT nMatInd;
  1646. /*?*/ ScMatrix* pResMat = GetNewMat(nC, nR, nMatInd);
  1647. /*?*/ if (pResMat)
  1648. /*?*/ {
  1649. /*?*/ ULONG nCount = (ULONG) nC * nR;
  1650. /*?*/ if (bFlag)
  1651. /*?*/ {
  1652. /*?*/ for ( ULONG i = 0; i < nCount; i++ )
  1653. /*?*/ if (!pMat->IsValue(i))
  1654. /*?*/ {
  1655. /*?*/ String sS = sStr;
  1656. /*?*/ sS += pMat->GetString(i);
  1657. /*?*/ pResMat->PutString(sS, i);
  1658. /*?*/ }
  1659. /*?*/ else
  1660. /*?*/ pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  1661. /*?*/ }
  1662. /*?*/ else
  1663. /*?*/ {
  1664. /*?*/ for ( ULONG i = 0; i < nCount; i++ )
  1665. /*?*/ if (!pMat->IsValue(i))
  1666. /*?*/ {
  1667. /*?*/ String sS = pMat->GetString(i);
  1668. /*?*/ sS += sStr;
  1669. /*?*/ pResMat->PutString(sS, i);
  1670. /*?*/ }
  1671. /*?*/ else
  1672. /*?*/ pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  1673. /*?*/ }
  1674. /*?*/ PushMatrix(pResMat);
  1675. /*?*/ nRetMat = nMatInd;
  1676. /*?*/ }
  1677. /*?*/ else
  1678. /*?*/ SetNoValue();
  1679. /*N*/ }
  1680. /*N*/ else
  1681. /*N*/ {
  1682. /*N*/ if ( CheckStringResultLen( sStr1, sStr2 ) )
  1683. /*N*/ sStr1 += sStr2;
  1684. /*N*/ PushString(sStr1);
  1685. /*N*/ }
  1686. /*N*/ }
  1687. /*N*/ void ScInterpreter::ScSub()
  1688. /*N*/ {
  1689. /*N*/ ScMatrix* pMat1 = NULL;
  1690. /*N*/ ScMatrix* pMat2 = NULL;
  1691. /*N*/ double fVal1 = 0.0, fVal2 = 0.0;
  1692. /*N*/ USHORT nMatInd1, nMatInd2;
  1693. /*N*/ short nFmt1, nFmt2;
  1694. /*N*/ nFmt1 = nFmt2 = NUMBERFORMAT_UNDEFINED;
  1695. /*N*/ short nFmtCurrencyType = nCurFmtType;
  1696. /*N*/ ULONG nFmtCurrencyIndex = nCurFmtIndex;
  1697. /*N*/ short nFmtPercentType = nCurFmtType;
  1698. /*N*/ MatrixDoubleRefToMatrix();
  1699. /*N*/ if ( GetStackType() == svMatrix )
  1700. /*N*/ pMat2 = GetMatrix(nMatInd2);
  1701. /*N*/ else
  1702. /*N*/ {
  1703. /*N*/ fVal2 = GetDouble();
  1704. /*N*/ switch ( nCurFmtType )
  1705. /*N*/ {
  1706. /*N*/ case NUMBERFORMAT_DATE :
  1707. /*N*/ case NUMBERFORMAT_TIME :
  1708. /*N*/ case NUMBERFORMAT_DATETIME :
  1709. /*N*/ nFmt2 = nCurFmtType;
  1710. /*N*/ break;
  1711. /*N*/ case NUMBERFORMAT_CURRENCY :
  1712. /*N*/ nFmtCurrencyType = nCurFmtType;
  1713. /*N*/ nFmtCurrencyIndex = nCurFmtIndex;
  1714. /*N*/ break;
  1715. /*N*/ case NUMBERFORMAT_PERCENT :
  1716. /*N*/ nFmtPercentType = NUMBERFORMAT_PERCENT;
  1717. /*N*/ break;
  1718. /*N*/ }
  1719. /*N*/ }
  1720. /*N*/ MatrixDoubleRefToMatrix();
  1721. /*N*/ if ( GetStackType() == svMatrix )
  1722. /*?*/ pMat1 = GetMatrix(nMatInd1);
  1723. /*N*/ else
  1724. /*N*/ {
  1725. /*N*/ fVal1 = GetDouble();
  1726. /*N*/ switch ( nCurFmtType )
  1727. /*N*/ {
  1728. /*N*/ case NUMBERFORMAT_DATE :
  1729. /*N*/ case NUMBERFORMAT_TIME :
  1730. /*N*/ case NUMBERFORMAT_DATETIME :
  1731. /*N*/ nFmt1 = nCurFmtType;
  1732. /*N*/ break;
  1733. /*N*/ case NUMBERFORMAT_CURRENCY :
  1734. /*N*/ nFmtCurrencyType = nCurFmtType;
  1735. /*N*/ nFmtCurrencyIndex = nCurFmtIndex;
  1736. /*N*/ break;
  1737. /*N*/ case NUMBERFORMAT_PERCENT :
  1738. /*N*/ nFmtPercentType = NUMBERFORMAT_PERCENT;
  1739. /*N*/ break;
  1740. /*N*/ }
  1741. /*N*/ }
  1742. /*N*/ if (pMat1 && pMat2)
  1743. /*N*/ {
  1744. /*?*/ ScMatrix* pResMat = MatSub(pMat1, pMat2);
  1745. /*?*/ if (!pResMat)
  1746. /*?*/ SetNoValue();
  1747. /*?*/ else
  1748. /*?*/ PushMatrix(pResMat);
  1749. /*N*/ }
  1750. /*N*/ else if (pMat1 || pMat2)
  1751. /*N*/ {
  1752. /*N*/ double fVal;
  1753. /*N*/ BOOL bFlag;
  1754. /*N*/ ScMatrix* pMat = pMat1;
  1755. /*N*/ if (!pMat)
  1756. /*N*/ {
  1757. /*N*/ fVal = fVal1;
  1758. /*N*/ pMat = pMat2;
  1759. /*N*/ bFlag = TRUE; // double - Matrix
  1760. /*N*/ }
  1761. /*N*/ else
  1762. /*N*/ {
  1763. /*N*/ fVal = fVal2;
  1764. /*N*/ bFlag = FALSE; // Matrix - double
  1765. /*N*/ }
  1766. /*N*/ USHORT nC, nR;
  1767. /*N*/ pMat->GetDimensions(nC, nR);
  1768. /*N*/ USHORT nMatInd;
  1769. /*N*/ ScMatrix* pResMat = GetNewMat(nC, nR, nMatInd);
  1770. /*N*/ if (pResMat)
  1771. /*N*/ { // mehr klammern wg. compiler macke
  1772. /*N*/ ULONG nCount = (ULONG) nC * nR;
  1773. /*N*/ if (bFlag)
  1774. /*N*/ { for ( ULONG i = 0; i < nCount; i++ )
  1775. /*N*/ { if (pMat->IsValue(i))
  1776. /*N*/ pResMat->PutDouble( ::rtl::math::approxSub( fVal, pMat->GetDouble(i)), i);
  1777. /*N*/ else
  1778. /*?*/ pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  1779. /*N*/ }
  1780. /*N*/ }
  1781. /*N*/ else
  1782. /*?*/ { for ( ULONG i = 0; i < nCount; i++ )
  1783. /*?*/ { if (pMat->IsValue(i))
  1784. /*?*/ pResMat->PutDouble( ::rtl::math::approxSub( pMat->GetDouble(i), fVal), i);
  1785. /*?*/ else
  1786. /*?*/ pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  1787. /*?*/ }
  1788. /*N*/ }
  1789. /*N*/ PushMatrix(pResMat);
  1790. /*N*/ nRetMat = nMatInd;
  1791. /*N*/ }
  1792. /*N*/ else
  1793. /*?*/ SetNoValue();
  1794. /*N*/ }
  1795. /*N*/ else
  1796. /*N*/ PushDouble( ::rtl::math::approxSub( fVal1, fVal2 ) );
  1797. /*N*/ if ( nFmtCurrencyType == NUMBERFORMAT_CURRENCY )
  1798. /*N*/ {
  1799. /*N*/ nFuncFmtType = nFmtCurrencyType;
  1800. /*N*/ nFuncFmtIndex = nFmtCurrencyIndex;
  1801. /*N*/ }
  1802. /*N*/ else
  1803. /*N*/ {
  1804. /*N*/ lcl_GetDiffDateTimeFmtType( nFuncFmtType, nFmt1, nFmt2 );
  1805. /*N*/ if ( nFmtPercentType == NUMBERFORMAT_PERCENT && nFuncFmtType == NUMBERFORMAT_NUMBER )
  1806. /*N*/ nFuncFmtType = NUMBERFORMAT_PERCENT;
  1807. /*N*/ }
  1808. /*N*/ }
  1809. /*N*/ void ScInterpreter::ScMul()
  1810. /*N*/ {
  1811. /*N*/ ScMatrix* pMat1 = NULL;
  1812. /*N*/ ScMatrix* pMat2 = NULL;
  1813. /*N*/ double fVal1 = 0.0, fVal2 = 0.0;
  1814. /*N*/ USHORT nMatInd1, nMatInd2;
  1815. /*N*/ short nFmtCurrencyType = nCurFmtType;
  1816. /*N*/ ULONG nFmtCurrencyIndex = nCurFmtIndex;
  1817. /*N*/ MatrixDoubleRefToMatrix();
  1818. /*N*/ if ( GetStackType() == svMatrix )
  1819. /*N*/ pMat2 = GetMatrix(nMatInd2);
  1820. /*N*/ else
  1821. /*N*/ {
  1822. /*N*/ fVal2 = GetDouble();
  1823. /*N*/ switch ( nCurFmtType )
  1824. /*N*/ {
  1825. /*N*/ case NUMBERFORMAT_CURRENCY :
  1826. /*N*/ nFmtCurrencyType = nCurFmtType;
  1827. /*N*/ nFmtCurrencyIndex = nCurFmtIndex;
  1828. /*N*/ break;
  1829. /*N*/ }
  1830. /*N*/ }
  1831. /*N*/ MatrixDoubleRefToMatrix();
  1832. /*N*/ if ( GetStackType() == svMatrix )
  1833. /*N*/ pMat1 = GetMatrix(nMatInd1);
  1834. /*N*/ else
  1835. /*N*/ {
  1836. /*N*/ fVal1 = GetDouble();
  1837. /*N*/ switch ( nCurFmtType )
  1838. /*N*/ {
  1839. /*N*/ case NUMBERFORMAT_CURRENCY :
  1840. /*N*/ nFmtCurrencyType = nCurFmtType;
  1841. /*N*/ nFmtCurrencyIndex = nCurFmtIndex;
  1842. /*N*/ break;
  1843. /*N*/ }
  1844. /*N*/ }
  1845. /*N*/ if (pMat1 && pMat2)
  1846. /*N*/ {
  1847. /*N*/ ScMatrix* pResMat = MatMul(pMat1, pMat2);
  1848. /*N*/ if (!pResMat)
  1849. /*?*/ SetNoValue();
  1850. /*N*/ else
  1851. /*N*/ PushMatrix(pResMat);
  1852. /*N*/ }
  1853. /*N*/ else if (pMat1 || pMat2)
  1854. /*N*/ {
  1855. /*N*/ double fVal;
  1856. /*N*/ ScMatrix* pMat = pMat1;
  1857. /*N*/ if (!pMat)
  1858. /*N*/ {
  1859. /*N*/ fVal = fVal1;
  1860. /*N*/ pMat = pMat2;
  1861. /*N*/ }
  1862. /*N*/ else
  1863. /*N*/ fVal = fVal2;
  1864. /*N*/ USHORT nC, nR;
  1865. /*N*/ pMat->GetDimensions(nC, nR);
  1866. /*N*/ USHORT nMatInd;
  1867. /*N*/ ScMatrix* pResMat = GetNewMat(nC, nR, nMatInd);
  1868. /*N*/ if (pResMat)
  1869. /*N*/ {
  1870. /*N*/ ULONG nCount = (ULONG) nC * nR;
  1871. /*N*/ for ( ULONG i = 0; i < nCount; i++ )
  1872. /*N*/ if (pMat->IsValue(i))
  1873. /*N*/ pResMat->PutDouble(pMat->GetDouble(i)*fVal, i);
  1874. /*N*/ else
  1875. /*N*/ pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  1876. /*N*/ PushMatrix(pResMat);
  1877. /*N*/ nRetMat = nMatInd;
  1878. /*N*/ }
  1879. /*N*/ else
  1880. /*?*/ SetNoValue();
  1881. /*N*/ }
  1882. /*N*/ else
  1883. /*N*/ PushDouble(fVal1 * fVal2);
  1884. /*N*/ if ( nFmtCurrencyType == NUMBERFORMAT_CURRENCY )
  1885. /*N*/ {
  1886. /*N*/ nFuncFmtType = nFmtCurrencyType;
  1887. /*N*/ nFuncFmtIndex = nFmtCurrencyIndex;
  1888. /*N*/ }
  1889. /*N*/ }
  1890. /*N*/ void ScInterpreter::ScDiv()
  1891. /*N*/ {
  1892. /*N*/ ScMatrix* pMat1 = NULL;
  1893. /*N*/ ScMatrix* pMat2 = NULL;
  1894. /*N*/ double fVal1 = 0.0, fVal2 = 0.0;
  1895. /*N*/ USHORT nMatInd1, nMatInd2;
  1896. /*N*/ short nFmtCurrencyType = nCurFmtType;
  1897. /*N*/ ULONG nFmtCurrencyIndex = nCurFmtIndex;
  1898. /*N*/ short nFmtCurrencyType2 = NUMBERFORMAT_UNDEFINED;
  1899. /*N*/ MatrixDoubleRefToMatrix();
  1900. /*N*/ if ( GetStackType() == svMatrix )
  1901. /*?*/ pMat2 = GetMatrix(nMatInd2);
  1902. /*N*/ else
  1903. /*N*/ {
  1904. /*N*/ fVal2 = GetDouble();
  1905. /*N*/ // hier kein Currency uebernehmen, 123kg/456DM sind nicht DM
  1906. /*N*/ nFmtCurrencyType2 = nCurFmtType;
  1907. /*N*/ }
  1908. /*N*/ MatrixDoubleRefToMatrix();
  1909. /*N*/ if ( GetStackType() == svMatrix )
  1910. /*?*/ pMat1 = GetMatrix(nMatInd1);
  1911. /*N*/ else
  1912. /*N*/ {
  1913. /*N*/ fVal1 = GetDouble();
  1914. /*N*/ switch ( nCurFmtType )
  1915. /*N*/ {
  1916. /*N*/ case NUMBERFORMAT_CURRENCY :
  1917. /*N*/ nFmtCurrencyType = nCurFmtType;
  1918. /*N*/ nFmtCurrencyIndex = nCurFmtIndex;
  1919. /*N*/ break;
  1920. /*N*/ }
  1921. /*N*/ }
  1922. /*N*/ if (pMat1 && pMat2)
  1923. /*N*/ {
  1924. /*?*/ ScMatrix* pResMat = MatDiv(pMat1, pMat2);
  1925. /*?*/ if (!pResMat)
  1926. /*?*/ SetNoValue();
  1927. /*?*/ else
  1928. /*?*/ PushMatrix(pResMat);
  1929. /*N*/ }
  1930. /*N*/ else if (pMat1 || pMat2)
  1931. /*N*/ {
  1932. /*?*/ double fVal;
  1933. /*?*/ BOOL bFlag;
  1934. /*?*/ ScMatrix* pMat = pMat1;
  1935. /*?*/ if (!pMat)
  1936. /*?*/ {
  1937. /*?*/ fVal = fVal1;
  1938. /*?*/ pMat = pMat2;
  1939. /*?*/ bFlag = TRUE; // double - Matrix
  1940. /*?*/ }
  1941. /*?*/ else
  1942. /*?*/ {
  1943. /*?*/ fVal = fVal2;
  1944. /*?*/ bFlag = FALSE; // Matrix - double
  1945. /*?*/ }
  1946. /*?*/ USHORT nC, nR;
  1947. /*?*/ pMat->GetDimensions(nC, nR);
  1948. /*?*/ USHORT nMatInd;
  1949. /*?*/ ScMatrix* pResMat = GetNewMat(nC, nR, nMatInd);
  1950. /*?*/ if (pResMat)
  1951. /*?*/ {
  1952. /*?*/ ULONG nCount = (ULONG) nC * nR;
  1953. /*?*/ if (bFlag)
  1954. /*?*/ { for ( ULONG i = 0; i < nCount; i++ )
  1955. /*?*/ if (pMat->IsValue(i))
  1956. /*?*/ pResMat->PutDouble(fVal / pMat->GetDouble(i), i);
  1957. /*?*/ else
  1958. /*?*/ pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  1959. /*?*/ }
  1960. /*?*/ else
  1961. /*?*/ { for ( ULONG i = 0; i < nCount; i++ )
  1962. /*?*/ if (pMat->IsValue(i))
  1963. /*?*/ pResMat->PutDouble(pMat->GetDouble(i) / fVal, i);
  1964. /*?*/ else
  1965. /*?*/ pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  1966. /*?*/ }
  1967. /*?*/ PushMatrix(pResMat);
  1968. /*?*/ nRetMat = nMatInd;
  1969. /*?*/ }
  1970. /*?*/ else
  1971. /*?*/ SetNoValue();
  1972. /*N*/ }
  1973. /*N*/ else
  1974. /*N*/ PushDouble(fVal1 / fVal2);
  1975. /*N*/ if ( nFmtCurrencyType == NUMBERFORMAT_CURRENCY && nFmtCurrencyType2 != NUMBERFORMAT_CURRENCY )
  1976. /*N*/ { // auch DM/DM ist nicht DM bzw. DEM/EUR nicht DEM
  1977. /*N*/ nFuncFmtType = nFmtCurrencyType;
  1978. /*N*/ nFuncFmtIndex = nFmtCurrencyIndex;
  1979. /*N*/ }
  1980. /*N*/ }
  1981. void ScInterpreter::ScPower()
  1982. {
  1983. if ( MustHaveParamCount( GetByte(), 2 ) )
  1984. ScPow();
  1985. }
  1986. void ScInterpreter::ScPow()
  1987. {
  1988. ScMatrix* pMat1 = NULL;
  1989. ScMatrix* pMat2 = NULL;
  1990. double fVal1 = 0.0, fVal2 = 0.0;
  1991. USHORT nMatInd1, nMatInd2;
  1992. MatrixDoubleRefToMatrix();
  1993. if ( GetStackType() == svMatrix )
  1994. pMat2 = GetMatrix(nMatInd2);
  1995. else
  1996. fVal2 = GetDouble();
  1997. MatrixDoubleRefToMatrix();
  1998. if ( GetStackType() == svMatrix )
  1999. pMat1 = GetMatrix(nMatInd1);
  2000. else
  2001. fVal1 = GetDouble();
  2002. if (pMat1 && pMat2)
  2003. {
  2004. ScMatrix* pResMat = MatPow(pMat1, pMat2);
  2005. if (!pResMat)
  2006. SetNoValue();
  2007. else
  2008. PushMatrix(pResMat);
  2009. }
  2010. else if (pMat1 || pMat2)
  2011. {
  2012. double fVal;
  2013. BOOL bFlag;
  2014. ScMatrix* pMat = pMat1;
  2015. if (!pMat)
  2016. {
  2017. fVal = fVal1;
  2018. pMat = pMat2;
  2019. bFlag = TRUE; // double - Matrix
  2020. }
  2021. else
  2022. {
  2023. fVal = fVal2;
  2024. bFlag = FALSE; // Matrix - double
  2025. }
  2026. USHORT nC, nR;
  2027. pMat->GetDimensions(nC, nR);
  2028. USHORT nMatInd;
  2029. ScMatrix* pResMat = GetNewMat(nC, nR, nMatInd);
  2030. if (pResMat)
  2031. {
  2032. ULONG nCount = (ULONG) nC * nR;
  2033. if (bFlag)
  2034. { for ( ULONG i = 0; i < nCount; i++ )
  2035. if (pMat->IsValue(i))
  2036. pResMat->PutDouble(pow(fVal,pMat->GetDouble(i)), i);
  2037. else
  2038. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  2039. }
  2040. else
  2041. { for ( ULONG i = 0; i < nCount; i++ )
  2042. if (pMat->IsValue(i))
  2043. pResMat->PutDouble(pow(pMat->GetDouble(i),fVal), i);
  2044. else
  2045. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i);
  2046. }
  2047. PushMatrix(pResMat);
  2048. nRetMat = nMatInd;
  2049. }
  2050. else
  2051. SetNoValue();
  2052. }
  2053. else
  2054. PushDouble(pow(fVal1,fVal2));
  2055. }
  2056. void ScInterpreter::ScSumProduct()
  2057. {
  2058. BYTE nParamCount = GetByte();
  2059. if ( !MustHaveParamCount( nParamCount, 1, 30 ) )
  2060. return;
  2061. ScMatrix* pMat1 = NULL;
  2062. ScMatrix* pMat2 = NULL;
  2063. ScMatrix* pMat = NULL;
  2064. USHORT nMatInd1, nMatInd2;
  2065. pMat2 = GetMatrix(nMatInd2);
  2066. if (!pMat2)
  2067. {
  2068. SetIllegalParameter();
  2069. return;
  2070. }
  2071. USHORT nC, nR, nC1, nR1;
  2072. pMat2->GetDimensions(nC, nR);
  2073. pMat = pMat2;
  2074. for (USHORT i = 1; i < nParamCount; i++)
  2075. {
  2076. pMat1 = GetMatrix(nMatInd1);
  2077. if (!pMat1)
  2078. {
  2079. SetIllegalParameter();
  2080. return;
  2081. }
  2082. pMat1->GetDimensions(nC1, nR1);
  2083. if (nC1 != nC || nR1 != nR)
  2084. {
  2085. SetNoValue();
  2086. return;
  2087. }
  2088. ScMatrix* pResMat = MatMul(pMat1, pMat);
  2089. if (!pResMat)
  2090. {
  2091. SetNoValue();
  2092. return;
  2093. }
  2094. else
  2095. pMat = pResMat;
  2096. }
  2097. double fSum = 0.0;
  2098. ULONG nCount = pMat->GetElementCount();
  2099. for (ULONG j = 0; j < nCount; j++)
  2100. if (!pMat->IsString(j))
  2101. fSum += pMat->GetDouble(j);
  2102. PushDouble(fSum);
  2103. }
  2104. void ScInterpreter::ScSumX2MY2()
  2105. {
  2106. if ( !MustHaveParamCount( GetByte(), 2 ) )
  2107. return;
  2108. ScMatrix* pMat1 = NULL;
  2109. ScMatrix* pMat2 = NULL;
  2110. USHORT nMatInd1, nMatInd2, i, j;
  2111. pMat2 = GetMatrix(nMatInd2);
  2112. pMat1 = GetMatrix(nMatInd1);
  2113. if (!pMat2 || !pMat1)
  2114. {
  2115. SetIllegalParameter();
  2116. return;
  2117. }
  2118. USHORT nC2, nR2, nC1, nR1;
  2119. pMat2->GetDimensions(nC2, nR2);
  2120. pMat1->GetDimensions(nC1, nR1);
  2121. if (nC1 != nC2 || nR1 != nR2)
  2122. {
  2123. SetNoValue();
  2124. return;
  2125. }
  2126. double fVal, fSum = 0.0;
  2127. for (i = 0; i < nC1; i++)
  2128. for (j = 0; j < nR1; j++)
  2129. if (!pMat1->IsString(i,j) && !pMat2->IsString(i,j))
  2130. {
  2131. fVal = pMat1->GetDouble(i,j);
  2132. fSum += fVal * fVal;
  2133. fVal = pMat2->GetDouble(i,j);
  2134. fSum -= fVal * fVal;
  2135. }
  2136. PushDouble(fSum);
  2137. }
  2138. void ScInterpreter::ScSumX2DY2()
  2139. {
  2140. if ( !MustHaveParamCount( GetByte(), 2 ) )
  2141. return;
  2142. ScMatrix* pMat1 = NULL;
  2143. ScMatrix* pMat2 = NULL;
  2144. USHORT nMatInd1, nMatInd2, i, j;
  2145. pMat2 = GetMatrix(nMatInd2);
  2146. pMat1 = GetMatrix(nMatInd1);
  2147. if (!pMat2 || !pMat1)
  2148. {
  2149. SetIllegalParameter();
  2150. return;
  2151. }
  2152. USHORT nC2, nR2, nC1, nR1;
  2153. pMat2->GetDimensions(nC2, nR2);
  2154. pMat1->GetDimensions(nC1, nR1);
  2155. if (nC1 != nC2 || nR1 != nR2)
  2156. {
  2157. SetNoValue();
  2158. return;
  2159. }
  2160. double fVal, fSum = 0.0;
  2161. for (i = 0; i < nC1; i++)
  2162. for (j = 0; j < nR1; j++)
  2163. if (!pMat1->IsString(i,j) && !pMat2->IsString(i,j))
  2164. {
  2165. fVal = pMat1->GetDouble(i,j);
  2166. fSum += fVal * fVal;
  2167. fVal = pMat2->GetDouble(i,j);
  2168. fSum += fVal * fVal;
  2169. }
  2170. PushDouble(fSum);
  2171. }
  2172. void ScInterpreter::ScSumXMY2()
  2173. {
  2174. if ( !MustHaveParamCount( GetByte(), 2 ) )
  2175. return;
  2176. ScMatrix* pMat1 = NULL;
  2177. ScMatrix* pMat2 = NULL;
  2178. USHORT nMatInd1, nMatInd2;
  2179. pMat2 = GetMatrix(nMatInd2);
  2180. pMat1 = GetMatrix(nMatInd1);
  2181. if (!pMat2 || !pMat1)
  2182. {
  2183. SetIllegalParameter();
  2184. return;
  2185. }
  2186. USHORT nC2, nR2, nC1, nR1;
  2187. pMat2->GetDimensions(nC2, nR2);
  2188. pMat1->GetDimensions(nC1, nR1);
  2189. if (nC1 != nC2 || nR1 != nR2)
  2190. {
  2191. SetNoValue();
  2192. return;
  2193. }
  2194. ScMatrix* pResMat = MatSub(pMat1, pMat2);
  2195. if (!pResMat)
  2196. {
  2197. SetNoValue();
  2198. return;
  2199. }
  2200. else
  2201. {
  2202. double fVal, fSum = 0.0;
  2203. ULONG nCount = pResMat->GetElementCount();
  2204. for (ULONG i = 0; i < nCount; i++)
  2205. if (!pResMat->IsString(i))
  2206. {
  2207. fVal = pResMat->GetDouble(i);
  2208. fSum += fVal * fVal;
  2209. }
  2210. PushDouble(fSum);
  2211. }
  2212. }
  2213. void ScInterpreter::ScFrequency()
  2214. {
  2215. if ( !MustHaveParamCount( GetByte(), 2 ) )
  2216. return;
  2217. USHORT nMatInd;
  2218. double* pSortArray1 = NULL;
  2219. ULONG nSize1 = 0;
  2220. GetSortArray(1, &pSortArray1, nSize1);
  2221. if (nGlobalError)
  2222. SetNoValue();
  2223. double* pSortArray2 = NULL;
  2224. ULONG nSize2 = 0;
  2225. GetSortArray(1, &pSortArray2, nSize2);
  2226. if (!pSortArray2 || nSize2 == 0 || nGlobalError)
  2227. {
  2228. if (pSortArray1)
  2229. delete pSortArray1;
  2230. if (pSortArray2)
  2231. delete pSortArray2;
  2232. SetNoValue();
  2233. return;
  2234. }
  2235. ScMatrix* pResMat = GetNewMat(1,(USHORT)nSize1+1,nMatInd);
  2236. if (!pResMat)
  2237. {
  2238. if (pSortArray1)
  2239. delete pSortArray1;
  2240. if (pSortArray2)
  2241. delete pSortArray2;
  2242. SetNoValue();
  2243. return;
  2244. }
  2245. USHORT j;
  2246. ULONG i = 0;
  2247. ULONG nCount;
  2248. for (j = 0; j < nSize1; j++)
  2249. {
  2250. nCount = 0;
  2251. while (i < nSize2 && pSortArray2[i] <= pSortArray1[j])
  2252. {
  2253. nCount++;
  2254. i++;
  2255. }
  2256. pResMat->PutDouble((double) nCount, j);
  2257. }
  2258. pResMat->PutDouble((double) (nSize2-i), j);
  2259. if (pSortArray1)
  2260. delete pSortArray1;
  2261. if (pSortArray2)
  2262. delete pSortArray2;
  2263. PushMatrix(pResMat);
  2264. nRetMat = nMatInd;
  2265. }
  2266. BOOL ScInterpreter::RGetVariances( ScMatrix* pV, ScMatrix* pX,
  2267. USHORT nC, USHORT nR, BOOL bSwapColRow, BOOL bZeroConstant )
  2268. { // multiple Regression: Varianzen der Koeffizienten
  2269. // bSwapColRow==TRUE : Koeffizienten in Zeilen statt Spalten angeordnet
  2270. USHORT i, j, k, nMatInd;
  2271. double sum;
  2272. ScMatrix* pC = GetNewMat(nC, nC, nMatInd);
  2273. if ( !pC )
  2274. return FALSE;
  2275. // X transformiert mit X multipziert, X'X Matrix
  2276. if ( !bZeroConstant )
  2277. { // in der X-Designmatrix existiert ein gedachtes X0j==1
  2278. if ( bSwapColRow )
  2279. {
  2280. for ( i=0; i<nC; i++ )
  2281. {
  2282. for ( j=0; j<nC; j++ )
  2283. {
  2284. sum = 0.0;
  2285. for ( k=0; k<nR; k++ )
  2286. {
  2287. sum += (j==0 ? 1 : pX->GetDouble(k,j-1))
  2288. * (i==0 ? 1 : pX->GetDouble(k,i-1));
  2289. }
  2290. pC->PutDouble(sum, i, j);
  2291. }
  2292. }
  2293. }
  2294. else
  2295. {
  2296. for ( i=0; i<nC; i++ )
  2297. {
  2298. for ( j=0; j<nC; j++ )
  2299. {
  2300. sum = 0.0;
  2301. for ( k=0; k<nR; k++ )
  2302. {
  2303. sum += (j==0 ? 1 : pX->GetDouble(j-1,k))
  2304. * (i==0 ? 1 : pX->GetDouble(i-1,k));
  2305. }
  2306. pC->PutDouble(sum, i, j);
  2307. }
  2308. }
  2309. }
  2310. }
  2311. else
  2312. {
  2313. if ( bSwapColRow )
  2314. {
  2315. for ( i=0; i<nC; i++ )
  2316. {
  2317. for ( j=0; j<nC; j++ )
  2318. {
  2319. sum = 0.0;
  2320. for ( k=0; k<nR; k++ )
  2321. {
  2322. sum += pX->GetDouble(k,j) * pX->GetDouble(k,i);
  2323. }
  2324. pC->PutDouble(sum, i, j);
  2325. }
  2326. }
  2327. }
  2328. else
  2329. {
  2330. for ( i=0; i<nC; i++ )
  2331. {
  2332. for ( j=0; j<nC; j++ )
  2333. {
  2334. sum = 0.0;
  2335. for ( k=0; k<nR; k++ )
  2336. {
  2337. sum += pX->GetDouble(j,k) * pX->GetDouble(i,k);
  2338. }
  2339. pC->PutDouble(sum, i, j);
  2340. }
  2341. }
  2342. }
  2343. }
  2344. // X'X Inverse
  2345. BOOL bOk = TRUE;
  2346. USHORT nErr = nGlobalError;
  2347. PushMatrix(pC);
  2348. BYTE nTmp = cPar;
  2349. cPar = 1;
  2350. ScMatInv();
  2351. cPar = nTmp;
  2352. if ( nGlobalError )
  2353. {
  2354. nGlobalError = nErr;
  2355. bOk = FALSE;
  2356. }
  2357. else
  2358. {
  2359. Pop(); // pC bleibt erhalten
  2360. // Varianzen auf der Diagonalen, andere sind Kovarianzen
  2361. for (i = 0; i < nC; i++)
  2362. pV->PutDouble(pC->GetDouble(i, i), i);
  2363. }
  2364. delete pC;
  2365. ResetNewMat(nMatInd);
  2366. return bOk;
  2367. }
  2368. void ScInterpreter::ScRGP()
  2369. {
  2370. BYTE nParamCount = GetByte();
  2371. if ( !MustHaveParamCount( nParamCount, 1, 4 ) )
  2372. return;
  2373. BOOL bConstant, bStats;
  2374. if (nParamCount == 4)
  2375. bStats = GetBool();
  2376. else
  2377. bStats = FALSE;
  2378. if (nParamCount >= 3)
  2379. bConstant = GetBool();
  2380. else
  2381. bConstant = TRUE;
  2382. USHORT nMatInd1, nMatInd2;
  2383. ScMatrix* pMatX;
  2384. ScMatrix* pMatY;
  2385. if (nParamCount >= 2)
  2386. pMatX = GetMatrix(nMatInd2);
  2387. else
  2388. pMatX = NULL;
  2389. pMatY = GetMatrix(nMatInd1);
  2390. if (!pMatY)
  2391. {
  2392. SetIllegalParameter();
  2393. return;
  2394. }
  2395. BYTE nCase; // 1 = normal, 2,3 = mehrfach
  2396. USHORT nCX, nRX, nCY, nRY, M, N;
  2397. pMatY->GetDimensions(nCY, nRY);
  2398. ULONG nCountY = (ULONG) nCY * nRY;
  2399. for ( ULONG i = 0; i < nCountY; i++ )
  2400. if (!pMatY->IsValue(i))
  2401. {
  2402. SetIllegalArgument();
  2403. return;
  2404. }
  2405. if (pMatX)
  2406. {
  2407. pMatX->GetDimensions(nCX, nRX);
  2408. ULONG nCountX = (ULONG) nCX * nRX;
  2409. for ( ULONG i = 0; i < nCountX; i++ )
  2410. if (!pMatX->IsValue(i))
  2411. {
  2412. SetIllegalArgument();
  2413. return;
  2414. }
  2415. if (nCX == nCY && nRX == nRY)
  2416. nCase = 1; // einfache Regression
  2417. else if (nCY != 1 && nRY != 1)
  2418. {
  2419. SetIllegalParameter();
  2420. return;
  2421. }
  2422. else if (nCY == 1)
  2423. {
  2424. if (nRX != nRY)
  2425. {
  2426. SetIllegalParameter();
  2427. return;
  2428. }
  2429. else
  2430. {
  2431. nCase = 2; // zeilenweise
  2432. N = nRY;
  2433. M = nCX;
  2434. }
  2435. }
  2436. else if (nCX != nCY)
  2437. {
  2438. SetIllegalParameter();
  2439. return;
  2440. }
  2441. else
  2442. {
  2443. nCase = 3; // spaltenweise
  2444. N = nCY;
  2445. M = nRX;
  2446. }
  2447. }
  2448. else
  2449. {
  2450. pMatX = GetNewMat(nCY, nRY, nMatInd2);
  2451. if (!pMatX)
  2452. {
  2453. SetIllegalParameter();
  2454. return;
  2455. }
  2456. for ( ULONG i = 1; i <= nCountY; i++ )
  2457. pMatX->PutDouble((double)i, i-1);
  2458. nCase = 1;
  2459. }
  2460. USHORT nMatInd;
  2461. ScMatrix* pResMat;
  2462. if (nCase == 1)
  2463. {
  2464. if (!bStats)
  2465. pResMat = GetNewMat(2,1,nMatInd);
  2466. else
  2467. pResMat = GetNewMat(2,5,nMatInd);
  2468. if (!pResMat)
  2469. {
  2470. SetIllegalParameter();
  2471. return;
  2472. }
  2473. double fCount = 0.0;
  2474. double fSumX = 0.0;
  2475. double fSumSqrX = 0.0;
  2476. double fSumY = 0.0;
  2477. double fSumSqrY = 0.0;
  2478. double fSumXY = 0.0;
  2479. double fValX, fValY;
  2480. for (USHORT i = 0; i < nCY; i++)
  2481. for (USHORT j = 0; j < nRY; j++)
  2482. {
  2483. fValX = pMatX->GetDouble(i,j);
  2484. fValY = pMatY->GetDouble(i,j);
  2485. fSumX += fValX;
  2486. fSumSqrX += fValX * fValX;
  2487. fSumY += fValY;
  2488. fSumSqrY += fValY * fValY;
  2489. fSumXY += fValX*fValY;
  2490. fCount++;
  2491. }
  2492. if (fCount < 1.0)
  2493. SetNoValue();
  2494. else
  2495. {
  2496. double f1 = fCount*fSumXY-fSumX*fSumY;
  2497. double fX = fCount*fSumSqrX-fSumX*fSumX;
  2498. double b, m;
  2499. if (bConstant)
  2500. {
  2501. b = fSumY/fCount - f1/fX*fSumX/fCount;
  2502. m = f1/fX;
  2503. }
  2504. else
  2505. {
  2506. b = 0.0;
  2507. m = fSumXY/fSumSqrX;
  2508. }
  2509. pResMat->PutDouble(m, 0, 0);
  2510. pResMat->PutDouble(b, 1, 0);
  2511. if (bStats)
  2512. {
  2513. double fY = fCount*fSumSqrY-fSumY*fSumY;
  2514. double fSyx = fSumSqrY-b*fSumY-m*fSumXY;
  2515. double fR2 = f1*f1/(fX*fY);
  2516. pResMat->PutDouble (fR2, 0, 2);
  2517. if (fCount < 3.0)
  2518. {
  2519. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 1 );
  2520. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 1, 1 );
  2521. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 1, 2 );
  2522. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 3 );
  2523. }
  2524. else
  2525. {
  2526. pResMat->PutDouble(sqrt(fSyx*fCount/(fX*(fCount-2.0))), 0, 1);
  2527. pResMat->PutDouble(sqrt(fSyx*fSumSqrX/fX/(fCount-2.0)), 1, 1);
  2528. pResMat->PutDouble(
  2529. sqrt((fCount*fSumSqrY - fSumY*fSumY - f1*f1/fX)/
  2530. (fCount*(fCount-2.0))), 1, 2);
  2531. if (fR2 == 1.0)
  2532. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 3 );
  2533. else
  2534. pResMat->PutDouble(fR2*(fCount-2.0)/(1.0-fR2), 0, 3);
  2535. }
  2536. pResMat->PutDouble(((double)(nCY*nRY))-2.0, 1, 3);
  2537. pResMat->PutDouble(fY/fCount-fSyx, 0, 4);
  2538. pResMat->PutDouble(fSyx, 1, 4);
  2539. }
  2540. }
  2541. }
  2542. else
  2543. {
  2544. USHORT nMatInd10, nMatInd12, nMatInd13, i, j, k;
  2545. if (!bStats)
  2546. pResMat = GetNewMat(M+1,1,nMatInd);
  2547. else
  2548. pResMat = GetNewMat(M+1,5,nMatInd);
  2549. if (!pResMat)
  2550. {
  2551. SetIllegalParameter();
  2552. return;
  2553. }
  2554. BOOL bVariancesOk = TRUE;
  2555. ScMatrix* pQ = GetNewMat(M+1, M+2, nMatInd10);
  2556. ScMatrix* pE = GetNewMat(M+2, 1, nMatInd12);
  2557. ScMatrix* pV = GetNewMat(M+1, 1, nMatInd13);
  2558. pE->PutDouble(0.0, M+1);
  2559. pQ->FillDouble(0.0, 0, 0, M, M+1);
  2560. if (nCase == 2)
  2561. {
  2562. for (k = 0; k < N; k++)
  2563. {
  2564. double Yk = pMatY->GetDouble(k);
  2565. pE->PutDouble( pE->GetDouble(M+1)+Yk*Yk, M+1 );
  2566. double sumYk = pQ->GetDouble(0, M+1) + Yk;
  2567. pQ->PutDouble( sumYk, 0, M+1 );
  2568. pE->PutDouble( sumYk, 0 );
  2569. for (i = 0; i < M; i++)
  2570. {
  2571. double Xik = pMatX->GetDouble(i,k);
  2572. double sumXik = pQ->GetDouble(0, i+1) + Xik;
  2573. pQ->PutDouble( sumXik, 0, i+1);
  2574. pQ->PutDouble( sumXik, i+1, 0);
  2575. double sumXikYk = pQ->GetDouble(i+1, M+1) + Xik * Yk;
  2576. pQ->PutDouble( sumXikYk, i+1, M+1);
  2577. pE->PutDouble( sumXikYk, i+1);
  2578. for (j = i; j < M; j++)
  2579. {
  2580. double sumXikXjk = pQ->GetDouble(j+1, i+1) +
  2581. Xik * pMatX->GetDouble(j,k);
  2582. pQ->PutDouble( sumXikXjk, j+1, i+1);
  2583. pQ->PutDouble( sumXikXjk, i+1, j+1);
  2584. }
  2585. }
  2586. }
  2587. }
  2588. else
  2589. {
  2590. for (k = 0; k < N; k++)
  2591. {
  2592. double Yk = pMatY->GetDouble(k);
  2593. pE->PutDouble( pE->GetDouble(M+1)+Yk*Yk, M+1 );
  2594. double sumYk = pQ->GetDouble(0, M+1) + Yk;
  2595. pQ->PutDouble( sumYk, 0, M+1 );
  2596. pE->PutDouble( sumYk, 0 );
  2597. for (i = 0; i < M; i++)
  2598. {
  2599. double Xki = pMatX->GetDouble(k,i);
  2600. double sumXki = pQ->GetDouble(0, i+1) + Xki;
  2601. pQ->PutDouble( sumXki, 0, i+1);
  2602. pQ->PutDouble( sumXki, i+1, 0);
  2603. double sumXkiYk = pQ->GetDouble(i+1, M+1) + Xki * Yk;
  2604. pQ->PutDouble( sumXkiYk, i+1, M+1);
  2605. pE->PutDouble( sumXkiYk, i+1);
  2606. for (j = i; j < M; j++)
  2607. {
  2608. double sumXkiXkj = pQ->GetDouble(j+1, i+1) +
  2609. Xki * pMatX->GetDouble(k,j);
  2610. pQ->PutDouble( sumXkiXkj, j+1, i+1);
  2611. pQ->PutDouble( sumXkiXkj, i+1, j+1);
  2612. }
  2613. }
  2614. }
  2615. }
  2616. pQ->PutDouble((double)N, 0, 0);
  2617. if (bConstant)
  2618. {
  2619. USHORT S, L;
  2620. for (S = 0; S < M+1; S++)
  2621. {
  2622. i = S;
  2623. while (i < M+1 && pQ->GetDouble(i, S) == 0.0)
  2624. i++;
  2625. if (i >= M+1)
  2626. {
  2627. SetNoValue();
  2628. delete pQ;
  2629. delete pE;
  2630. delete pV;
  2631. ResetNewMat(nMatInd13);
  2632. ResetNewMat(nMatInd12);
  2633. ResetNewMat(nMatInd10);
  2634. return;
  2635. }
  2636. double fVal;
  2637. for (L = 0; L < M+2; L++)
  2638. {
  2639. fVal = pQ->GetDouble(S, L);
  2640. pQ->PutDouble(pQ->GetDouble(i, L), S, L);
  2641. pQ->PutDouble(fVal, i, L);
  2642. }
  2643. fVal = 1.0/pQ->GetDouble(S, S);
  2644. for (L = 0; L < M+2; L++)
  2645. pQ->PutDouble(pQ->GetDouble(S, L)*fVal, S, L);
  2646. for (i = 0; i < M+1; i++)
  2647. {
  2648. if (i != S)
  2649. {
  2650. fVal = -pQ->GetDouble(i, S);
  2651. for (L = 0; L < M+2; L++)
  2652. pQ->PutDouble(
  2653. pQ->GetDouble(i,L)+fVal*pQ->GetDouble(S,L),i,L);
  2654. }
  2655. }
  2656. }
  2657. }
  2658. else
  2659. {
  2660. USHORT S, L;
  2661. for (S = 1; S < M+1; S++)
  2662. {
  2663. i = S;
  2664. while (i < M+1 && pQ->GetDouble(i, S) == 0.0)
  2665. i++;
  2666. if (i >= M+1)
  2667. {
  2668. SetNoValue();
  2669. delete pQ;
  2670. delete pE;
  2671. delete pV;
  2672. ResetNewMat(nMatInd13);
  2673. ResetNewMat(nMatInd12);
  2674. ResetNewMat(nMatInd10);
  2675. return;
  2676. }
  2677. double fVal;
  2678. for (L = 1; L < M+2; L++)
  2679. {
  2680. fVal = pQ->GetDouble(S, L);
  2681. pQ->PutDouble(pQ->GetDouble(i, L), S, L);
  2682. pQ->PutDouble(fVal, i, L);
  2683. }
  2684. fVal = 1.0/pQ->GetDouble(S, S);
  2685. for (L = 1; L < M+2; L++)
  2686. pQ->PutDouble(pQ->GetDouble(S, L)*fVal, S, L);
  2687. for (i = 1; i < M+1; i++)
  2688. {
  2689. if (i != S)
  2690. {
  2691. fVal = -pQ->GetDouble(i, S);
  2692. for (L = 1; L < M+2; L++)
  2693. pQ->PutDouble(
  2694. pQ->GetDouble(i,L)+fVal*pQ->GetDouble(S,L),i,L);
  2695. }
  2696. }
  2697. pQ->PutDouble(0.0, 0, M+1); // Konstante b
  2698. }
  2699. }
  2700. // mn ... m1, b
  2701. for (i = 0; i < M+1; i++)
  2702. pResMat->PutDouble(pQ->GetDouble(M-i,M+1), i, 0);
  2703. if (bStats)
  2704. {
  2705. // pE[0] := Sigma i=1...n (Yi)
  2706. // pE[k] := Sigma i=1...n (Xki*Yi)
  2707. // pE[M+1] := Sigma i=1...n (Yi**2)
  2708. // pQ[0,M+1]:= B
  2709. // pQ[k,M+1]:= Mk
  2710. double fSQR, fSQT, fSQE;
  2711. fSQT = pE->GetDouble(M+1)
  2712. - pE->GetDouble(0) * pE->GetDouble(0) / (double)N;
  2713. fSQR = pE->GetDouble(M+1);
  2714. for (i = 0; i < M+1; i++)
  2715. fSQR -= pQ->GetDouble(i, M+1) * pE->GetDouble(i);
  2716. fSQE = fSQT-fSQR;
  2717. // r2 (Bestimmtheitsmass, 0...1)
  2718. if (fSQT == 0.0)
  2719. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 2);
  2720. else
  2721. pResMat->PutDouble (fSQE/fSQT, 0, 2);
  2722. // ssReg (Regressions-Quadratsumme)
  2723. pResMat->PutDouble(fSQE, 0, 4);
  2724. // ssResid (Residual-Quadratsumme, Summe der Abweichungsquadrate)
  2725. pResMat->PutDouble(fSQR, 1, 4);
  2726. for (i = 2; i < 5; i++)
  2727. for (j = 2; j < M+1; j++)
  2728. pResMat->PutString(ScGlobal::GetRscString(STR_NV_STR), j, i);
  2729. if (bConstant)
  2730. {
  2731. if (N-M-1 == 0)
  2732. {
  2733. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 1, 2);
  2734. for (i = 0; i < M+1; i++)
  2735. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i, 1);
  2736. }
  2737. else
  2738. {
  2739. double fSE2 = fSQR/(N-M-1);
  2740. // sey (Standardfehler des Schaetzwertes y)
  2741. pResMat->PutDouble(sqrt(fSE2), 1, 2);
  2742. // sen...se1 (Standardfehler der Koeffizienten mn...m1)
  2743. // seb (Standardfehler der Konstanten b)
  2744. if ( RGetVariances( pV, pMatX, M+1, N, nCase != 2, FALSE ) )
  2745. {
  2746. for (i = 0; i < M+1; i++)
  2747. pResMat->PutDouble( sqrt(fSE2 * pV->GetDouble(i)), M-i, 1 );
  2748. }
  2749. else
  2750. {
  2751. for (i = 0; i < M+1; i++)
  2752. pResMat->PutString(ScGlobal::GetRscString(STR_NV_STR), i, 1);
  2753. }
  2754. }
  2755. // F (F-Statistik)
  2756. if (fSQR == 0.0)
  2757. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 3);
  2758. else
  2759. pResMat->PutDouble(((double)(N-M-1))*fSQE/fSQR/((double)M),0, 3);
  2760. // df (Freiheitsgrad)
  2761. pResMat->PutDouble(((double)(N-M-1)), 1, 3);
  2762. }
  2763. else
  2764. {
  2765. if (N-M == 0)
  2766. {
  2767. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 1, 2);
  2768. for (i = 0; i < M+1; i++)
  2769. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i, 1);
  2770. }
  2771. else
  2772. {
  2773. double fSE2 = fSQR/(N-M);
  2774. pResMat->PutDouble(sqrt(fSE2), 1, 2);
  2775. if ( RGetVariances( pV, pMatX, M, N, nCase != 2, TRUE ) )
  2776. {
  2777. for (i = 0; i < M; i++)
  2778. pResMat->PutDouble( sqrt(fSE2 * pV->GetDouble(i)), M-i-1, 1 );
  2779. pResMat->PutString(ScGlobal::GetRscString(STR_NV_STR), M, 1);
  2780. }
  2781. else
  2782. {
  2783. for (i = 0; i < M+1; i++)
  2784. pResMat->PutString(ScGlobal::GetRscString(STR_NV_STR), i, 1);
  2785. }
  2786. }
  2787. if (fSQR == 0.0)
  2788. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 3);
  2789. else
  2790. pResMat->PutDouble(((double)(N-M))*fSQE/fSQR/((double)M),0, 3);
  2791. pResMat->PutDouble(((double)(N-M)), 1, 3);
  2792. }
  2793. }
  2794. delete pQ;
  2795. delete pE;
  2796. delete pV;
  2797. ResetNewMat(nMatInd13);
  2798. ResetNewMat(nMatInd12);
  2799. ResetNewMat(nMatInd10);
  2800. }
  2801. PushMatrix(pResMat);
  2802. nRetMat = nMatInd;
  2803. }
  2804. void ScInterpreter::ScRKP()
  2805. {
  2806. BYTE nParamCount = GetByte();
  2807. if ( !MustHaveParamCount( nParamCount, 1, 4 ) )
  2808. return;
  2809. BOOL bConstant, bStats;
  2810. if (nParamCount == 4)
  2811. bStats = GetBool();
  2812. else
  2813. bStats = FALSE;
  2814. if (nParamCount >= 3)
  2815. bConstant = GetBool();
  2816. else
  2817. bConstant = TRUE;
  2818. USHORT nMatInd1, nMatInd2;
  2819. ScMatrix* pMatX;
  2820. ScMatrix* pMatY;
  2821. if (nParamCount >= 2)
  2822. pMatX = GetMatrix(nMatInd2);
  2823. else
  2824. pMatX = NULL;
  2825. pMatY = GetMatrix(nMatInd1);
  2826. if (!pMatY)
  2827. {
  2828. SetIllegalParameter();
  2829. return;
  2830. }
  2831. BYTE nCase; // 1 = normal, 2,3 = mehrfach
  2832. USHORT nCX, nRX, nCY, nRY, M, N;
  2833. pMatY->GetDimensions(nCY, nRY);
  2834. ULONG nCountY = (ULONG) nCY * nRY;
  2835. ULONG i;
  2836. for (i = 0; i < nCountY; i++)
  2837. if (!pMatY->IsValue(i))
  2838. {
  2839. SetIllegalArgument();
  2840. return;
  2841. }
  2842. for (i = 0; i < nCountY; i++)
  2843. {
  2844. if (pMatY->GetDouble(i) <= 0.0)
  2845. {
  2846. SetIllegalArgument();
  2847. return;
  2848. }
  2849. else
  2850. pMatY->PutDouble(log(pMatY->GetDouble(i)), i);
  2851. }
  2852. if (pMatX)
  2853. {
  2854. pMatX->GetDimensions(nCX, nRX);
  2855. ULONG nCountX = (ULONG) nCX * nRX;
  2856. for (i = 0; i < nCountX; i++)
  2857. if (!pMatX->IsValue(i))
  2858. {
  2859. SetIllegalArgument();
  2860. return;
  2861. }
  2862. if (nCX == nCY && nRX == nRY)
  2863. nCase = 1; // einfache Regression
  2864. else if (nCY != 1 && nRY != 1)
  2865. {
  2866. SetIllegalParameter();
  2867. return;
  2868. }
  2869. else if (nCY == 1)
  2870. {
  2871. if (nRX != nRY)
  2872. {
  2873. SetIllegalParameter();
  2874. return;
  2875. }
  2876. else
  2877. {
  2878. nCase = 2; // zeilenweise
  2879. N = nRY;
  2880. M = nCX;
  2881. }
  2882. }
  2883. else if (nCX != nCY)
  2884. {
  2885. SetIllegalParameter();
  2886. return;
  2887. }
  2888. else
  2889. {
  2890. nCase = 3; // spaltenweise
  2891. N = nCY;
  2892. M = nRX;
  2893. }
  2894. }
  2895. else
  2896. {
  2897. pMatX = GetNewMat(nCY, nRY, nMatInd2);
  2898. if (!pMatX)
  2899. {
  2900. SetIllegalParameter();
  2901. return;
  2902. }
  2903. for ( ULONG i = 1; i <= nCountY; i++ )
  2904. pMatX->PutDouble((double)i, i-1);
  2905. nCase = 1;
  2906. }
  2907. USHORT nMatInd;
  2908. ScMatrix* pResMat;
  2909. if (nCase == 1)
  2910. {
  2911. if (!bStats)
  2912. pResMat = GetNewMat(2,1,nMatInd);
  2913. else
  2914. pResMat = GetNewMat(2,5,nMatInd);
  2915. if (!pResMat)
  2916. {
  2917. SetIllegalParameter();
  2918. return;
  2919. }
  2920. double fCount = 0.0;
  2921. double fSumX = 0.0;
  2922. double fSumSqrX = 0.0;
  2923. double fSumY = 0.0;
  2924. double fSumSqrY = 0.0;
  2925. double fSumXY = 0.0;
  2926. double fValX, fValY;
  2927. for (USHORT i = 0; i < nCY; i++)
  2928. for (USHORT j = 0; j < nRY; j++)
  2929. {
  2930. fValX = pMatX->GetDouble(i,j);
  2931. fValY = pMatY->GetDouble(i,j);
  2932. fSumX += fValX;
  2933. fSumSqrX += fValX * fValX;
  2934. fSumY += fValY;
  2935. fSumSqrY += fValY * fValY;
  2936. fSumXY += fValX*fValY;
  2937. fCount++;
  2938. }
  2939. if (fCount < 1.0)
  2940. SetNoValue();
  2941. else
  2942. {
  2943. double f1 = fCount*fSumXY-fSumX*fSumY;
  2944. double fX = fCount*fSumSqrX-fSumX*fSumX;
  2945. double b, m;
  2946. if (bConstant)
  2947. {
  2948. b = fSumY/fCount - f1/fX*fSumX/fCount;
  2949. m = f1/fX;
  2950. }
  2951. else
  2952. {
  2953. b = 0.0;
  2954. m = fSumXY/fSumSqrX;
  2955. }
  2956. pResMat->PutDouble(exp(m), 0, 0);
  2957. pResMat->PutDouble(exp(b), 1, 0);
  2958. if (bStats)
  2959. {
  2960. double fY = fCount*fSumSqrY-fSumY*fSumY;
  2961. double fSyx = fSumSqrY-b*fSumY-m*fSumXY;
  2962. double fR2 = f1*f1/(fX*fY);
  2963. pResMat->PutDouble (fR2, 0, 2);
  2964. if (fCount < 3.0)
  2965. {
  2966. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 1 );
  2967. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 1, 1 );
  2968. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 1, 2 );
  2969. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 3 );
  2970. }
  2971. else
  2972. {
  2973. pResMat->PutDouble(sqrt(fSyx*fCount/(fX*(fCount-2.0))), 0, 1);
  2974. pResMat->PutDouble(sqrt(fSyx*fSumSqrX/fX/(fCount-2.0)), 1, 1);
  2975. pResMat->PutDouble(
  2976. sqrt((fCount*fSumSqrY - fSumY*fSumY - f1*f1/fX)/
  2977. (fCount*(fCount-2.0))), 1, 2);
  2978. if (fR2 == 1.0)
  2979. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 3 );
  2980. else
  2981. pResMat->PutDouble(fR2*(fCount-2.0)/(1.0-fR2), 0, 3);
  2982. }
  2983. pResMat->PutDouble(((double)(nCY*nRY))-2.0, 1, 3);
  2984. pResMat->PutDouble(fY/fCount-fSyx, 0, 4);
  2985. pResMat->PutDouble(fSyx, 1, 4);
  2986. }
  2987. }
  2988. }
  2989. else
  2990. {
  2991. USHORT nMatInd10, nMatInd12, nMatInd13, i, j, k;
  2992. if (!bStats)
  2993. pResMat = GetNewMat(M+1,1,nMatInd);
  2994. else
  2995. pResMat = GetNewMat(M+1,5,nMatInd);
  2996. if (!pResMat)
  2997. {
  2998. SetIllegalParameter();
  2999. return;
  3000. }
  3001. ScMatrix* pQ = GetNewMat(M+1, M+2, nMatInd10);
  3002. ScMatrix* pE = GetNewMat(M+2, 1, nMatInd12);
  3003. ScMatrix* pV = GetNewMat(M+1, 1, nMatInd13);
  3004. pE->PutDouble(0.0, M+1);
  3005. pQ->FillDouble(0.0, 0, 0, M, M+1);
  3006. if (nCase == 2)
  3007. {
  3008. for (k = 0; k < N; k++)
  3009. {
  3010. double Yk = pMatY->GetDouble(k);
  3011. pE->PutDouble( pE->GetDouble(M+1)+Yk*Yk, M+1 );
  3012. double sumYk = pQ->GetDouble(0, M+1) + Yk;
  3013. pQ->PutDouble( sumYk, 0, M+1 );
  3014. pE->PutDouble( sumYk, 0 );
  3015. for (i = 0; i < M; i++)
  3016. {
  3017. double Xik = pMatX->GetDouble(i,k);
  3018. double sumXik = pQ->GetDouble(0, i+1) + Xik;
  3019. pQ->PutDouble( sumXik, 0, i+1);
  3020. pQ->PutDouble( sumXik, i+1, 0);
  3021. double sumXikYk = pQ->GetDouble(i+1, M+1) + Xik * Yk;
  3022. pQ->PutDouble( sumXikYk, i+1, M+1);
  3023. pE->PutDouble( sumXikYk, i+1);
  3024. for (j = i; j < M; j++)
  3025. {
  3026. double sumXikXjk = pQ->GetDouble(j+1, i+1) +
  3027. Xik * pMatX->GetDouble(j,k);
  3028. pQ->PutDouble( sumXikXjk, j+1, i+1);
  3029. pQ->PutDouble( sumXikXjk, i+1, j+1);
  3030. }
  3031. }
  3032. }
  3033. }
  3034. else
  3035. {
  3036. for (k = 0; k < N; k++)
  3037. {
  3038. double Yk = pMatY->GetDouble(k);
  3039. pE->PutDouble( pE->GetDouble(M+1)+Yk*Yk, M+1 );
  3040. double sumYk = pQ->GetDouble(0, M+1) + Yk;
  3041. pQ->PutDouble( sumYk, 0, M+1 );
  3042. pE->PutDouble( sumYk, 0 );
  3043. for (i = 0; i < M; i++)
  3044. {
  3045. double Xki = pMatX->GetDouble(k,i);
  3046. double sumXki = pQ->GetDouble(0, i+1) + Xki;
  3047. pQ->PutDouble( sumXki, 0, i+1);
  3048. pQ->PutDouble( sumXki, i+1, 0);
  3049. double sumXkiYk = pQ->GetDouble(i+1, M+1) + Xki * Yk;
  3050. pQ->PutDouble( sumXkiYk, i+1, M+1);
  3051. pE->PutDouble( sumXkiYk, i+1);
  3052. for (j = i; j < M; j++)
  3053. {
  3054. double sumXkiXkj = pQ->GetDouble(j+1, i+1) +
  3055. Xki * pMatX->GetDouble(k,j);
  3056. pQ->PutDouble( sumXkiXkj, j+1, i+1);
  3057. pQ->PutDouble( sumXkiXkj, i+1, j+1);
  3058. }
  3059. }
  3060. }
  3061. }
  3062. pQ->PutDouble((double)N, 0, 0);
  3063. if (bConstant)
  3064. {
  3065. USHORT S, L;
  3066. for (S = 0; S < M+1; S++)
  3067. {
  3068. i = S;
  3069. while (i < M+1 && pQ->GetDouble(i, S) == 0.0)
  3070. i++;
  3071. if (i >= M+1)
  3072. {
  3073. SetNoValue();
  3074. delete pQ;
  3075. delete pE;
  3076. delete pV;
  3077. ResetNewMat(nMatInd13);
  3078. ResetNewMat(nMatInd12);
  3079. ResetNewMat(nMatInd10);
  3080. return;
  3081. }
  3082. double fVal;
  3083. for (L = 0; L < M+2; L++)
  3084. {
  3085. fVal = pQ->GetDouble(S, L);
  3086. pQ->PutDouble(pQ->GetDouble(i, L), S, L);
  3087. pQ->PutDouble(fVal, i, L);
  3088. }
  3089. fVal = 1.0/pQ->GetDouble(S, S);
  3090. for (L = 0; L < M+2; L++)
  3091. pQ->PutDouble(pQ->GetDouble(S, L)*fVal, S, L);
  3092. for (i = 0; i < M+1; i++)
  3093. {
  3094. if (i != S)
  3095. {
  3096. fVal = -pQ->GetDouble(i, S);
  3097. for (L = 0; L < M+2; L++)
  3098. pQ->PutDouble(
  3099. pQ->GetDouble(i,L)+fVal*pQ->GetDouble(S,L),i,L);
  3100. }
  3101. }
  3102. }
  3103. }
  3104. else
  3105. {
  3106. USHORT S, L;
  3107. for (S = 1; S < M+1; S++)
  3108. {
  3109. i = S;
  3110. while (i < M+1 && pQ->GetDouble(i, S) == 0.0)
  3111. i++;
  3112. if (i >= M+1)
  3113. {
  3114. SetNoValue();
  3115. delete pQ;
  3116. delete pE;
  3117. delete pV;
  3118. ResetNewMat(nMatInd13);
  3119. ResetNewMat(nMatInd12);
  3120. ResetNewMat(nMatInd10);
  3121. return;
  3122. }
  3123. double fVal;
  3124. for (L = 1; L < M+2; L++)
  3125. {
  3126. fVal = pQ->GetDouble(S, L);
  3127. pQ->PutDouble(pQ->GetDouble(i, L), S, L);
  3128. pQ->PutDouble(fVal, i, L);
  3129. }
  3130. fVal = 1.0/pQ->GetDouble(S, S);
  3131. for (L = 1; L < M+2; L++)
  3132. pQ->PutDouble(pQ->GetDouble(S, L)*fVal, S, L);
  3133. for (i = 1; i < M+1; i++)
  3134. {
  3135. if (i != S)
  3136. {
  3137. fVal = -pQ->GetDouble(i, S);
  3138. for (L = 1; L < M+2; L++)
  3139. pQ->PutDouble(
  3140. pQ->GetDouble(i,L)+fVal*pQ->GetDouble(S,L),i,L);
  3141. }
  3142. }
  3143. pQ->PutDouble(0.0, 0, M+1);
  3144. }
  3145. }
  3146. for (i = 0; i < M+1; i++)
  3147. pResMat->PutDouble(exp(pQ->GetDouble(M-i,M+1)), i, 0);
  3148. if (bStats)
  3149. {
  3150. double fSQR, fSQT, fSQE;
  3151. fSQT = pE->GetDouble(M+1)-pE->GetDouble(0)*pE->GetDouble(0)/((double)N);
  3152. fSQR = pE->GetDouble(M+1);
  3153. for (i = 0; i < M+1; i++)
  3154. fSQR += -pQ->GetDouble(i, M+1)*pE->GetDouble(i);
  3155. fSQE = fSQT-fSQR;
  3156. if (fSQT == 0.0)
  3157. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 2);
  3158. else
  3159. pResMat->PutDouble (fSQE/fSQT, 0, 2);
  3160. pResMat->PutDouble(fSQE, 0, 4);
  3161. pResMat->PutDouble(fSQR, 1, 4);
  3162. for (i = 2; i < 5; i++)
  3163. for (j = 2; j < M+1; j++)
  3164. pResMat->PutString(ScGlobal::GetRscString(STR_NV_STR), j, i);
  3165. if (bConstant)
  3166. {
  3167. if (N-M-1 == 0)
  3168. {
  3169. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 1, 2);
  3170. for (i = 0; i < M+1; i++)
  3171. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i, 1);
  3172. }
  3173. else
  3174. {
  3175. double fSE2 = fSQR/(N-M-1);
  3176. pResMat->PutDouble(sqrt(fSE2), 1, 2);
  3177. if ( RGetVariances( pV, pMatX, M+1, N, nCase != 2, FALSE ) )
  3178. {
  3179. for (i = 0; i < M+1; i++)
  3180. pResMat->PutDouble( sqrt(fSE2 * pV->GetDouble(i)), M-i, 1 );
  3181. }
  3182. else
  3183. {
  3184. for (i = 0; i < M+1; i++)
  3185. pResMat->PutString(ScGlobal::GetRscString(STR_NV_STR), i, 1);
  3186. }
  3187. }
  3188. if (fSQR == 0.0)
  3189. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 3);
  3190. else
  3191. pResMat->PutDouble(((double)(N-M-1))*fSQE/fSQR/((double)M),0, 3);
  3192. pResMat->PutDouble(((double)(N-M-1)), 1, 3);
  3193. }
  3194. else
  3195. {
  3196. if (N-M == 0)
  3197. {
  3198. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 1, 2);
  3199. for (i = 0; i < M+1; i++)
  3200. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), i, 1);
  3201. }
  3202. else
  3203. {
  3204. double fSE2 = fSQR/(N-M);
  3205. pResMat->PutDouble(sqrt(fSE2), 1, 2);
  3206. if ( RGetVariances( pV, pMatX, M, N, nCase != 2, TRUE ) )
  3207. {
  3208. for (i = 0; i < M; i++)
  3209. pResMat->PutDouble( sqrt(fSE2 * pV->GetDouble(i)), M-i-1, 1 );
  3210. pResMat->PutString(ScGlobal::GetRscString(STR_NV_STR), M, 1);
  3211. }
  3212. else
  3213. {
  3214. for (i = 0; i < M+1; i++)
  3215. pResMat->PutString(ScGlobal::GetRscString(STR_NV_STR), i, 1);
  3216. }
  3217. }
  3218. if (fSQR == 0.0)
  3219. pResMat->PutString(ScGlobal::GetRscString(STR_NO_VALUE), 0, 3);
  3220. else
  3221. pResMat->PutDouble(((double)(N-M))*fSQE/fSQR/((double)M),0, 3);
  3222. pResMat->PutDouble(((double)(N-M)), 1, 3);
  3223. }
  3224. }
  3225. delete pQ;
  3226. delete pE;
  3227. delete pV;
  3228. ResetNewMat(nMatInd13);
  3229. ResetNewMat(nMatInd12);
  3230. ResetNewMat(nMatInd10);
  3231. }
  3232. PushMatrix(pResMat);
  3233. nRetMat = nMatInd;
  3234. }
  3235. void ScInterpreter::ScTrend()
  3236. {
  3237. BYTE nParamCount = GetByte();
  3238. if ( !MustHaveParamCount( nParamCount, 1, 4 ) )
  3239. return;
  3240. BOOL bConstant;
  3241. if (nParamCount == 4)
  3242. bConstant = GetBool();
  3243. else
  3244. bConstant = TRUE;
  3245. USHORT nMatInd1, nMatInd2, nMatInd3;
  3246. ScMatrix* pMatX;
  3247. ScMatrix* pMatY;
  3248. ScMatrix* pMatNewX;
  3249. if (nParamCount >= 3)
  3250. pMatNewX = GetMatrix(nMatInd3);
  3251. else
  3252. pMatNewX = NULL;
  3253. if (nParamCount >= 2)
  3254. pMatX = GetMatrix(nMatInd2);
  3255. else
  3256. pMatX = NULL;
  3257. pMatY = GetMatrix(nMatInd1);
  3258. if (!pMatY)
  3259. {
  3260. SetIllegalParameter();
  3261. return;
  3262. }
  3263. BYTE nCase; // 1 = normal, 2,3 = mehrfach
  3264. USHORT nCX, nRX, nCY, nRY, M, N;
  3265. pMatY->GetDimensions(nCY, nRY);
  3266. ULONG nCountY = (ULONG) nCY * nRY;
  3267. ULONG i;
  3268. for (i = 0; i < nCountY; i++)
  3269. if (!pMatY->IsValue(i))
  3270. {
  3271. SetIllegalArgument();
  3272. return;
  3273. }
  3274. if (pMatX)
  3275. {
  3276. pMatX->GetDimensions(nCX, nRX);
  3277. ULONG nCountX = (ULONG) nCX * nRX;
  3278. for (i = 0; i < nCountX; i++)
  3279. if (!pMatX->IsValue(i))
  3280. {
  3281. SetIllegalArgument();
  3282. return;
  3283. }
  3284. if (nCX == nCY && nRX == nRY)
  3285. nCase = 1; // einfache Regression
  3286. else if (nCY != 1 && nRY != 1)
  3287. {
  3288. SetIllegalParameter();
  3289. return;
  3290. }
  3291. else if (nCY == 1)
  3292. {
  3293. if (nRX != nRY)
  3294. {
  3295. SetIllegalParameter();
  3296. return;
  3297. }
  3298. else
  3299. {
  3300. nCase = 2; // zeilenweise
  3301. N = nRY;
  3302. M = nCX;
  3303. }
  3304. }
  3305. else if (nCX != nCY)
  3306. {
  3307. SetIllegalParameter();
  3308. return;
  3309. }
  3310. else
  3311. {
  3312. nCase = 3; // spaltenweise
  3313. N = nCY;
  3314. M = nRX;
  3315. }
  3316. }
  3317. else
  3318. {
  3319. pMatX = GetNewMat(nCY, nRY, nMatInd2);
  3320. nCX = nCY;
  3321. nRX = nRY;
  3322. if (!pMatX)
  3323. {
  3324. SetIllegalParameter();
  3325. return;
  3326. }
  3327. for (i = 1; i <= nCountY; i++)
  3328. pMatX->PutDouble((double)i, i-1);
  3329. nCase = 1;
  3330. }
  3331. USHORT nCXN, nRXN;
  3332. ULONG nCountXN;
  3333. if (!pMatNewX)
  3334. {
  3335. nCXN = nCX;
  3336. nRXN = nRX;
  3337. nCountXN = (ULONG) nCXN * nRXN;
  3338. pMatNewX = pMatX;
  3339. }
  3340. else
  3341. {
  3342. pMatNewX->GetDimensions(nCXN, nRXN);
  3343. if ((nCase == 2 && nCX != nCXN) || (nCase == 3 && nRX != nRXN))
  3344. {
  3345. SetIllegalArgument();
  3346. return;
  3347. }
  3348. nCountXN = (ULONG) nCXN * nRXN;
  3349. for ( ULONG i = 0; i < nCountXN; i++ )
  3350. if (!pMatNewX->IsValue(i))
  3351. {
  3352. SetIllegalArgument();
  3353. return;
  3354. }
  3355. }
  3356. USHORT nMatInd;
  3357. ScMatrix* pResMat;
  3358. if (nCase == 1)
  3359. {
  3360. double fCount = 0.0;
  3361. double fSumX = 0.0;
  3362. double fSumSqrX = 0.0;
  3363. double fSumY = 0.0;
  3364. double fSumSqrY = 0.0;
  3365. double fSumXY = 0.0;
  3366. double fValX, fValY;
  3367. for (USHORT i = 0; i < nCY; i++)
  3368. for (USHORT j = 0; j < nRY; j++)
  3369. {
  3370. fValX = pMatX->GetDouble(i,j);
  3371. fValY = pMatY->GetDouble(i,j);
  3372. fSumX += fValX;
  3373. fSumSqrX += fValX * fValX;
  3374. fSumY += fValY;
  3375. fSumSqrY += fValY * fValY;
  3376. fSumXY += fValX*fValY;
  3377. fCount++;
  3378. }
  3379. if (fCount < 1.0)
  3380. {
  3381. SetNoValue();
  3382. return;
  3383. }
  3384. else
  3385. {
  3386. double f1 = fCount*fSumXY-fSumX*fSumY;
  3387. double fX = fCount*fSumSqrX-fSumX*fSumX;
  3388. double b, m;
  3389. if (bConstant)
  3390. {
  3391. b = fSumY/fCount - f1/fX*fSumX/fCount;
  3392. m = f1/fX;
  3393. }
  3394. else
  3395. {
  3396. b = 0.0;
  3397. m = fSumXY/fSumSqrX;
  3398. }
  3399. pResMat = GetNewMat(nCXN, nRXN, nMatInd);
  3400. if (!pResMat)
  3401. {
  3402. SetIllegalParameter();
  3403. return;
  3404. }
  3405. for (i = 0; i < nCountXN; i++)
  3406. pResMat->PutDouble(pMatNewX->GetDouble(i)*m+b, i);
  3407. }
  3408. }
  3409. else
  3410. {
  3411. USHORT nMatInd10, nMatInd12, i, j, k;
  3412. ScMatrix* pQ = GetNewMat(M+1, M+2, nMatInd10);
  3413. ScMatrix* pE = GetNewMat(M+2, 1, nMatInd12);
  3414. pE->PutDouble(0.0, M+1);
  3415. pQ->FillDouble(0.0, 0, 0, M, M+1);
  3416. if (nCase == 2)
  3417. {
  3418. for (k = 0; k < N; k++)
  3419. {
  3420. pE->PutDouble(
  3421. pE->GetDouble(M+1)+pMatY->GetDouble(k)*pMatY->GetDouble(k), M+1);
  3422. pQ->PutDouble(pQ->GetDouble(0, M+1) + pMatY->GetDouble(k), 0, M+1);
  3423. pE->PutDouble(pQ->GetDouble(0, M+1), 0);
  3424. for (i = 0; i < M; i++)
  3425. {
  3426. pQ->PutDouble(pQ->GetDouble(0, i+1)+pMatX->GetDouble(i,k), 0, i+1);
  3427. pQ->PutDouble(pQ->GetDouble(0, i+1), i+1, 0);
  3428. pQ->PutDouble(pQ->GetDouble(i+1, M+1) +
  3429. pMatX->GetDouble(i,k)*pMatY->GetDouble(k), i+1, M+1);
  3430. pE->PutDouble(pQ->GetDouble(i+1, M+1), i+1);
  3431. for (j = i; j < M; j++)
  3432. {
  3433. pQ->PutDouble(pQ->GetDouble(j+1, i+1) +
  3434. pMatX->GetDouble(i,k)*pMatX->GetDouble(j,k), j+1, i+1);
  3435. pQ->PutDouble(pQ->GetDouble(j+1, i+1), i+1, j+1);
  3436. }
  3437. }
  3438. }
  3439. }
  3440. else
  3441. {
  3442. for (k = 0; k < N; k++)
  3443. {
  3444. pE->PutDouble(
  3445. pE->GetDouble(M+1)+pMatY->GetDouble(k)*pMatY->GetDouble(k), M+1);
  3446. pQ->PutDouble(pQ->GetDouble(0, M+1) + pMatY->GetDouble(k), 0, M+1);
  3447. pE->PutDouble(pQ->GetDouble(0, M+1), 0);
  3448. for (i = 0; i < M; i++)
  3449. {
  3450. pQ->PutDouble(pQ->GetDouble(0, i+1)+pMatX->GetDouble(k,i), 0, i+1);
  3451. pQ->PutDouble(pQ->GetDouble(0, i+1), i+1, 0);
  3452. pQ->PutDouble(pQ->GetDouble(i+1, M+1) +
  3453. pMatX->GetDouble(k,i)*pMatY->GetDouble(k), i+1, M+1);
  3454. pE->PutDouble(pQ->GetDouble(i+1, M+1), i+1);
  3455. for (j = i; j < M; j++)
  3456. {
  3457. pQ->PutDouble(pQ->GetDouble(j+1, i+1) +
  3458. pMatX->GetDouble(k, i)*pMatX->GetDouble(k, j), j+1, i+1);
  3459. pQ->PutDouble(pQ->GetDouble(j+1, i+1), i+1, j+1);
  3460. }
  3461. }
  3462. }
  3463. }
  3464. pQ->PutDouble((double)N, 0, 0);
  3465. if (bConstant)
  3466. {
  3467. USHORT S, L;
  3468. for (S = 0; S < M+1; S++)
  3469. {
  3470. i = S;
  3471. while (i < M+1 && pQ->GetDouble(i, S) == 0.0)
  3472. i++;
  3473. if (i >= M+1)
  3474. {
  3475. SetNoValue();
  3476. delete pQ;
  3477. delete pE;
  3478. ResetNewMat(nMatInd12);
  3479. ResetNewMat(nMatInd10);
  3480. return;
  3481. }
  3482. double fVal;
  3483. for (L = 0; L < M+2; L++)
  3484. {
  3485. fVal = pQ->GetDouble(S, L);
  3486. pQ->PutDouble(pQ->GetDouble(i, L), S, L);
  3487. pQ->PutDouble(fVal, i, L);
  3488. }
  3489. fVal = 1.0/pQ->GetDouble(S, S);
  3490. for (L = 0; L < M+2; L++)
  3491. pQ->PutDouble(pQ->GetDouble(S, L)*fVal, S, L);
  3492. for (i = 0; i < M+1; i++)
  3493. {
  3494. if (i != S)
  3495. {
  3496. fVal = -pQ->GetDouble(i, S);
  3497. for (L = 0; L < M+2; L++)
  3498. pQ->PutDouble(
  3499. pQ->GetDouble(i,L)+fVal*pQ->GetDouble(S,L),i,L);
  3500. }
  3501. }
  3502. }
  3503. }
  3504. else
  3505. {
  3506. USHORT S, L;
  3507. for (S = 1; S < M+1; S++)
  3508. {
  3509. i = S;
  3510. while (i < M+1 && pQ->GetDouble(i, S) == 0.0)
  3511. i++;
  3512. if (i >= M+1)
  3513. {
  3514. SetNoValue();
  3515. delete pQ;
  3516. delete pE;
  3517. ResetNewMat(nMatInd12);
  3518. ResetNewMat(nMatInd10);
  3519. return;
  3520. }
  3521. double fVal;
  3522. for (L = 1; L < M+2; L++)
  3523. {
  3524. fVal = pQ->GetDouble(S, L);
  3525. pQ->PutDouble(pQ->GetDouble(i, L), S, L);
  3526. pQ->PutDouble(fVal, i, L);
  3527. }
  3528. fVal = 1.0/pQ->GetDouble(S, S);
  3529. for (L = 1; L < M+2; L++)
  3530. pQ->PutDouble(pQ->GetDouble(S, L)*fVal, S, L);
  3531. for (i = 1; i < M+1; i++)
  3532. {
  3533. if (i != S)
  3534. {
  3535. fVal = -pQ->GetDouble(i, S);
  3536. for (L = 1; L < M+2; L++)
  3537. pQ->PutDouble(
  3538. pQ->GetDouble(i,L)+fVal*pQ->GetDouble(S,L),i,L);
  3539. }
  3540. }
  3541. pQ->PutDouble(0.0, 0, M+1);
  3542. }
  3543. }
  3544. if (nCase == 2)
  3545. {
  3546. pResMat = GetNewMat(1, nRXN, nMatInd);
  3547. if (!pResMat)
  3548. {
  3549. SetIllegalParameter();
  3550. return;
  3551. }
  3552. double fVal;
  3553. for (i = 0; i < nRXN; i++)
  3554. {
  3555. fVal = pQ->GetDouble(0, M+1);
  3556. for (j = 0; j < M; j++)
  3557. fVal += pQ->GetDouble(j+1, M+1)*pMatNewX->GetDouble(j, i);
  3558. pResMat->PutDouble(fVal, i);
  3559. }
  3560. }
  3561. else
  3562. {
  3563. pResMat = GetNewMat(nCXN, 1, nMatInd);
  3564. if (!pResMat)
  3565. {
  3566. SetIllegalParameter();
  3567. return;
  3568. }
  3569. double fVal;
  3570. for (i = 0; i < nCXN; i++)
  3571. {
  3572. fVal = pQ->GetDouble(0, M+1);
  3573. for (j = 0; j < M; j++)
  3574. fVal += pQ->GetDouble(j+1, M+1)*pMatNewX->GetDouble(i, j);
  3575. pResMat->PutDouble(fVal, i);
  3576. }
  3577. }
  3578. delete pQ;
  3579. delete pE;
  3580. ResetNewMat(nMatInd12);
  3581. ResetNewMat(nMatInd10);
  3582. }
  3583. PushMatrix(pResMat);
  3584. nRetMat = nMatInd;
  3585. }
  3586. void ScInterpreter::ScGrowth()
  3587. {
  3588. BYTE nParamCount = GetByte();
  3589. if ( !MustHaveParamCount( nParamCount, 1, 4 ) )
  3590. return;
  3591. BOOL bConstant;
  3592. if (nParamCount == 4)
  3593. bConstant = GetBool();
  3594. else
  3595. bConstant = TRUE;
  3596. USHORT nMatInd1, nMatInd2, nMatInd3;
  3597. ScMatrix* pMatX;
  3598. ScMatrix* pMatY;
  3599. ScMatrix* pMatNewX;
  3600. if (nParamCount >= 3)
  3601. pMatNewX = GetMatrix(nMatInd3);
  3602. else
  3603. pMatNewX = NULL;
  3604. if (nParamCount >= 2)
  3605. pMatX = GetMatrix(nMatInd2);
  3606. else
  3607. pMatX = NULL;
  3608. pMatY = GetMatrix(nMatInd1);
  3609. if (!pMatY)
  3610. {
  3611. SetIllegalParameter();
  3612. return;
  3613. }
  3614. BYTE nCase; // 1 = normal, 2,3 = mehrfach
  3615. USHORT nCX, nRX, nCY, nRY, M, N;
  3616. pMatY->GetDimensions(nCY, nRY);
  3617. ULONG nCountY = (ULONG) nCY * nRY;
  3618. ULONG i;
  3619. for (i = 0; i < nCountY; i++)
  3620. if (!pMatY->IsValue(i))
  3621. {
  3622. SetIllegalArgument();
  3623. return;
  3624. }
  3625. for (i = 0; i < nCountY; i++)
  3626. {
  3627. if (pMatY->GetDouble(i) <= 0.0)
  3628. {
  3629. SetIllegalArgument();
  3630. return;
  3631. }
  3632. else
  3633. pMatY->PutDouble(log(pMatY->GetDouble(i)), i);
  3634. }
  3635. if (pMatX)
  3636. {
  3637. pMatX->GetDimensions(nCX, nRX);
  3638. ULONG nCountX = (ULONG) nCX * nRX;
  3639. for ( ULONG i = 0; i < nCountX; i++ )
  3640. if (!pMatX->IsValue(i))
  3641. {
  3642. SetIllegalArgument();
  3643. return;
  3644. }
  3645. if (nCX == nCY && nRX == nRY)
  3646. nCase = 1; // einfache Regression
  3647. else if (nCY != 1 && nRY != 1)
  3648. {
  3649. SetIllegalParameter();
  3650. return;
  3651. }
  3652. else if (nCY == 1)
  3653. {
  3654. if (nRX != nRY)
  3655. {
  3656. SetIllegalParameter();
  3657. return;
  3658. }
  3659. else
  3660. {
  3661. nCase = 2; // zeilenweise
  3662. N = nRY;
  3663. M = nCX;
  3664. }
  3665. }
  3666. else if (nCX != nCY)
  3667. {
  3668. SetIllegalParameter();
  3669. return;
  3670. }
  3671. else
  3672. {
  3673. nCase = 3; // spaltenweise
  3674. N = nCY;
  3675. M = nRX;
  3676. }
  3677. }
  3678. else
  3679. {
  3680. pMatX = GetNewMat(nCY, nRY, nMatInd2);
  3681. nCX = nCY;
  3682. nRX = nRY;
  3683. if (!pMatX)
  3684. {
  3685. SetIllegalParameter();
  3686. return;
  3687. }
  3688. for (i = 1; i <= nCountY; i++)
  3689. pMatX->PutDouble((double)i, i-1);
  3690. nCase = 1;
  3691. }
  3692. USHORT nCXN, nRXN;
  3693. ULONG nCountXN;
  3694. if (!pMatNewX)
  3695. {
  3696. nCXN = nCX;
  3697. nRXN = nRX;
  3698. nCountXN = (ULONG) nCXN * nRXN;
  3699. pMatNewX = pMatX;
  3700. }
  3701. else
  3702. {
  3703. pMatNewX->GetDimensions(nCXN, nRXN);
  3704. if ((nCase == 2 && nCX != nCXN) || (nCase == 3 && nRX != nRXN))
  3705. {
  3706. SetIllegalArgument();
  3707. return;
  3708. }
  3709. nCountXN = (ULONG) nCXN * nRXN;
  3710. for ( ULONG i = 0; i < nCountXN; i++ )
  3711. if (!pMatNewX->IsValue(i))
  3712. {
  3713. SetIllegalArgument();
  3714. return;
  3715. }
  3716. }
  3717. USHORT nMatInd;
  3718. ScMatrix* pResMat;
  3719. if (nCase == 1)
  3720. {
  3721. double fCount = 0.0;
  3722. double fSumX = 0.0;
  3723. double fSumSqrX = 0.0;
  3724. double fSumY = 0.0;
  3725. double fSumSqrY = 0.0;
  3726. double fSumXY = 0.0;
  3727. double fValX, fValY;
  3728. for (USHORT i = 0; i < nCY; i++)
  3729. for (USHORT j = 0; j < nRY; j++)
  3730. {
  3731. fValX = pMatX->GetDouble(i,j);
  3732. fValY = pMatY->GetDouble(i,j);
  3733. fSumX += fValX;
  3734. fSumSqrX += fValX * fValX;
  3735. fSumY += fValY;
  3736. fSumSqrY += fValY * fValY;
  3737. fSumXY += fValX*fValY;
  3738. fCount++;
  3739. }
  3740. if (fCount < 1.0)
  3741. {
  3742. SetNoValue();
  3743. return;
  3744. }
  3745. else
  3746. {
  3747. double f1 = fCount*fSumXY-fSumX*fSumY;
  3748. double fX = fCount*fSumSqrX-fSumX*fSumX;
  3749. double b, m;
  3750. if (bConstant)
  3751. {
  3752. b = fSumY/fCount - f1/fX*fSumX/fCount;
  3753. m = f1/fX;
  3754. }
  3755. else
  3756. {
  3757. b = 0.0;
  3758. m = fSumXY/fSumSqrX;
  3759. }
  3760. pResMat = GetNewMat(nCXN, nRXN, nMatInd);
  3761. if (!pResMat)
  3762. {
  3763. SetIllegalParameter();
  3764. return;
  3765. }
  3766. for (i = 0; i < nCountXN; i++)
  3767. pResMat->PutDouble(exp(pMatNewX->GetDouble(i)*m+b), i);
  3768. }
  3769. }
  3770. else
  3771. {
  3772. USHORT nMatInd10, nMatInd12, i, j, k;
  3773. ScMatrix* pQ = GetNewMat(M+1, M+2, nMatInd10);
  3774. ScMatrix* pE = GetNewMat(M+2, 1, nMatInd12);
  3775. pE->PutDouble(0.0, M+1);
  3776. pQ->FillDouble(0.0, 0, 0, M, M+1);
  3777. if (nCase == 2)
  3778. {
  3779. for (k = 0; k < N; k++)
  3780. {
  3781. pE->PutDouble(
  3782. pE->GetDouble(M+1)+pMatY->GetDouble(k)*pMatY->GetDouble(k), M+1);
  3783. pQ->PutDouble(pQ->GetDouble(0, M+1) + pMatY->GetDouble(k), 0, M+1);
  3784. pE->PutDouble(pQ->GetDouble(0, M+1), 0);
  3785. for (i = 0; i < M; i++)
  3786. {
  3787. pQ->PutDouble(pQ->GetDouble(0, i+1)+pMatX->GetDouble(i,k), 0, i+1);
  3788. pQ->PutDouble(pQ->GetDouble(0, i+1), i+1, 0);
  3789. pQ->PutDouble(pQ->GetDouble(i+1, M+1) +
  3790. pMatX->GetDouble(i,k)*pMatY->GetDouble(k), i+1, M+1);
  3791. pE->PutDouble(pQ->GetDouble(i+1, M+1), i+1);
  3792. for (j = i; j < M; j++)
  3793. {
  3794. pQ->PutDouble(pQ->GetDouble(j+1, i+1) +
  3795. pMatX->GetDouble(i,k)*pMatX->GetDouble(j,k), j+1, i+1);
  3796. pQ->PutDouble(pQ->GetDouble(j+1, i+1), i+1, j+1);
  3797. }
  3798. }
  3799. }
  3800. }
  3801. else
  3802. {
  3803. for (k = 0; k < N; k++)
  3804. {
  3805. pE->PutDouble(
  3806. pE->GetDouble(M+1)+pMatY->GetDouble(k)*pMatY->GetDouble(k), M+1);
  3807. pQ->PutDouble(pQ->GetDouble(0, M+1) + pMatY->GetDouble(k), 0, M+1);
  3808. pE->PutDouble(pQ->GetDouble(0, M+1), 0);
  3809. for (i = 0; i < M; i++)
  3810. {
  3811. pQ->PutDouble(pQ->GetDouble(0, i+1)+pMatX->GetDouble(k,i), 0, i+1);
  3812. pQ->PutDouble(pQ->GetDouble(0, i+1), i+1, 0);
  3813. pQ->PutDouble(pQ->GetDouble(i+1, M+1) +
  3814. pMatX->GetDouble(k,i)*pMatY->GetDouble(k), i+1, M+1);
  3815. pE->PutDouble(pQ->GetDouble(i+1, M+1), i+1);
  3816. for (j = i; j < M; j++)
  3817. {
  3818. pQ->PutDouble(pQ->GetDouble(j+1, i+1) +
  3819. pMatX->GetDouble(k, i)*pMatX->GetDouble(k, j), j+1, i+1);
  3820. pQ->PutDouble(pQ->GetDouble(j+1, i+1), i+1, j+1);
  3821. }
  3822. }
  3823. }
  3824. }
  3825. pQ->PutDouble((double)N, 0, 0);
  3826. if (bConstant)
  3827. {
  3828. USHORT S, L;
  3829. for (S = 0; S < M+1; S++)
  3830. {
  3831. i = S;
  3832. while (i < M+1 && pQ->GetDouble(i, S) == 0.0)
  3833. i++;
  3834. if (i >= M+1)
  3835. {
  3836. SetNoValue();
  3837. delete pQ;
  3838. delete pE;
  3839. ResetNewMat(nMatInd12);
  3840. ResetNewMat(nMatInd10);
  3841. return;
  3842. }
  3843. double fVal;
  3844. for (L = 0; L < M+2; L++)
  3845. {
  3846. fVal = pQ->GetDouble(S, L);
  3847. pQ->PutDouble(pQ->GetDouble(i, L), S, L);
  3848. pQ->PutDouble(fVal, i, L);
  3849. }
  3850. fVal = 1.0/pQ->GetDouble(S, S);
  3851. for (L = 0; L < M+2; L++)
  3852. pQ->PutDouble(pQ->GetDouble(S, L)*fVal, S, L);
  3853. for (i = 0; i < M+1; i++)
  3854. {
  3855. if (i != S)
  3856. {
  3857. fVal = -pQ->GetDouble(i, S);
  3858. for (L = 0; L < M+2; L++)
  3859. pQ->PutDouble(
  3860. pQ->GetDouble(i,L)+fVal*pQ->GetDouble(S,L),i,L);
  3861. }
  3862. }
  3863. }
  3864. }
  3865. else
  3866. {
  3867. USHORT S, L;
  3868. for (S = 1; S < M+1; S++)
  3869. {
  3870. i = S;
  3871. while (i < M+1 && pQ->GetDouble(i, S) == 0.0)
  3872. i++;
  3873. if (i >= M+1)
  3874. {
  3875. SetNoValue();
  3876. delete pQ;
  3877. delete pE;
  3878. ResetNewMat(nMatInd12);
  3879. ResetNewMat(nMatInd10);
  3880. return;
  3881. }
  3882. double fVal;
  3883. for (L = 1; L < M+2; L++)
  3884. {
  3885. fVal = pQ->GetDouble(S, L);
  3886. pQ->PutDouble(pQ->GetDouble(i, L), S, L);
  3887. pQ->PutDouble(fVal, i, L);
  3888. }
  3889. fVal = 1.0/pQ->GetDouble(S, S);
  3890. for (L = 1; L < M+2; L++)
  3891. pQ->PutDouble(pQ->GetDouble(S, L)*fVal, S, L);
  3892. for (i = 1; i < M+1; i++)
  3893. {
  3894. if (i != S)
  3895. {
  3896. fVal = -pQ->GetDouble(i, S);
  3897. for (L = 1; L < M+2; L++)
  3898. pQ->PutDouble(
  3899. pQ->GetDouble(i,L)+fVal*pQ->GetDouble(S,L),i,L);
  3900. }
  3901. }
  3902. pQ->PutDouble(0.0, 0, M+1);
  3903. }
  3904. }
  3905. if (nCase == 2)
  3906. {
  3907. pResMat = GetNewMat(1, nRXN, nMatInd);
  3908. if (!pResMat)
  3909. {
  3910. SetIllegalParameter();
  3911. return;
  3912. }
  3913. double fVal;
  3914. for (i = 0; i < nRXN; i++)
  3915. {
  3916. fVal = pQ->GetDouble(0, M+1);
  3917. for (j = 0; j < M; j++)
  3918. fVal += pQ->GetDouble(j+1, M+1)*pMatNewX->GetDouble(j, i);
  3919. pResMat->PutDouble(exp(fVal), i);
  3920. }
  3921. }
  3922. else
  3923. {
  3924. pResMat = GetNewMat(nCXN, 1, nMatInd);
  3925. if (!pResMat)
  3926. {
  3927. SetIllegalParameter();
  3928. return;
  3929. }
  3930. double fVal;
  3931. for (i = 0; i < nCXN; i++)
  3932. {
  3933. fVal = pQ->GetDouble(0, M+1);
  3934. for (j = 0; j < M; j++)
  3935. fVal += pQ->GetDouble(j+1, M+1)*pMatNewX->GetDouble(i, j);
  3936. pResMat->PutDouble(exp(fVal), i);
  3937. }
  3938. }
  3939. delete pQ;
  3940. delete pE;
  3941. ResetNewMat(nMatInd12);
  3942. ResetNewMat(nMatInd10);
  3943. }
  3944. PushMatrix(pResMat);
  3945. nRetMat = nMatInd;
  3946. }
  3947. /*N*/ void ScInterpreter::ScMatRef()
  3948. /*N*/ {
  3949. /*N*/ // Falls Deltarefs drin sind...
  3950. /*N*/ Push( (ScToken&) *pCur );
  3951. /*N*/ ScAddress aAdr;
  3952. /*N*/ PopSingleRef( aAdr );
  3953. /*N*/ ScFormulaCell* pCell = (ScFormulaCell*) GetCell( aAdr );
  3954. /*N*/ if( pCell && pCell->GetCellType() == CELLTYPE_FORMULA )
  3955. /*N*/ {
  3956. /*N*/ ScMatrix* pMat;
  3957. /*N*/ pCell->GetMatrix( &pMat );
  3958. /*N*/ if( pMat )
  3959. /*N*/ {
  3960. /*N*/ USHORT nCl, nRw;
  3961. /*N*/ pMat->GetDimensions( nCl, nRw );
  3962. /*N*/ USHORT nC = aPos.Col() - aAdr.Col();
  3963. /*N*/ USHORT nR = aPos.Row() - aAdr.Row();
  3964. /*N*/ if (nC < nCl && nR < nRw)
  3965. /*N*/ {
  3966. /*N*/ BOOL bIsString;
  3967. /*N*/ const MatValue* pMatVal = pMat->Get(nC, nR, bIsString);
  3968. /*N*/ if (bIsString)
  3969. /*?*/ PushString( pMatVal->GetString() );
  3970. /*N*/ else
  3971. /*N*/ {
  3972. /*N*/ PushDouble(pMatVal->fVal);
  3973. /*N*/ pDok->GetNumberFormatInfo( nCurFmtType, nCurFmtIndex, aAdr, *pCell );
  3974. /*N*/ nFuncFmtType = nCurFmtType;
  3975. /*N*/ nFuncFmtIndex = nCurFmtIndex;
  3976. /*N*/ }
  3977. /*N*/ }
  3978. /*N*/ else
  3979. /*?*/ SetNV();
  3980. /*N*/ }
  3981. /*N*/ else
  3982. /*N*/ {
  3983. /*?*/ // Ist gar keine Ergebnis-Matrix, dann bitte den Wert holen!
  3984. /*?*/ USHORT nErr = pCell->GetErrCode();
  3985. /*?*/ SetError( nErr );
  3986. /*?*/ if( pCell->IsValue() )
  3987. /*?*/ PushDouble( pCell->GetValue() );
  3988. /*?*/ else
  3989. /*?*/ {
  3990. /*?*/ String aVal;
  3991. /*?*/ pCell->GetString( aVal );
  3992. /*?*/ PushString( aVal );
  3993. /*?*/ }
  3994. /*?*/ pDok->GetNumberFormatInfo( nCurFmtType, nCurFmtIndex, aAdr, *pCell );
  3995. /*?*/ nFuncFmtType = nCurFmtType;
  3996. /*?*/ nFuncFmtIndex = nCurFmtIndex;
  3997. /*?*/ }
  3998. /*N*/ }
  3999. /*N*/ else
  4000. /*N*/ SetError( errNoRef );
  4001. /*N*/ }
  4002. }