PageRenderTime 89ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/libreoffice-3.6.0.2/binfilter/bf_sc/source/core/tool/sc_interpr5.cxx

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