Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Changes In Branch modifyprimals Excluding Merge-Ins
This is equivalent to a diff from 4dad0cad83 to 51732172c2
|
2019-12-21
| ||
| 22:48 | Merge release Leaf check-in: 51732172c2 user: fifr tags: modifyprimals | |
| 21:28 | Add `dyn` to trait object types check-in: 45d9ecf62b user: fifr tags: modifyprimals | |
| 21:07 | Update version to 0.6.3 check-in: e4a7214ded user: fifr tags: release, v0.6.3 | |
|
2018-08-30
| ||
| 13:31 | Merge error-handling check-in: a304098147 user: fifr tags: trunk | |
| 09:04 | Update version to 0.6.0-dev check-in: a4c479bbc1 user: fifr tags: modifyprimals | |
|
2018-08-18
| ||
| 11:15 | Reformat Closed-Leaf check-in: 4dad0cad83 user: fifr tags: error-handling | |
|
2018-07-09
| ||
| 09:32 | Add .editorconfig check-in: 7557d9d11d user: fifr tags: error-handling | |
Changes to .fossil-settings/ignore-glob.
1 2 3 4 | 1 2 3 4 5 | + | target/ *.log *.cpxlog Cargo.lock instances/ |
Changes to Cargo.toml.
1 2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | - + + - + - + - + | [package] name = "bundle" |
Changes to examples/mmcf.rs.
| ︙ | |||
11 12 13 14 15 16 17 | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | - - + + - - + + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/> */ |
| ︙ | |||
39 40 41 42 43 44 45 | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | + - + + - + |
mmcf,
SolverParams {
max_bundle_size: 25,
min_weight: 1e-3,
max_weight: 100.0,
..Default::default()
},
)
|
Changes to examples/quadratic.rs.
| ︙ | |||
13 14 15 16 17 18 19 | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | - - - + + - - + | * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/> */ use std::error::Error; |
| ︙ | |||
92 93 94 95 96 97 98 | 90 91 92 93 94 95 96 97 98 99 100 | + - + |
let mut solver = Solver::new_params(
f,
SolverParams {
min_weight: 1.0,
max_weight: 1.0,
..Default::default()
},
)
|
Changes to src/firstorderproblem.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | - + - - + + |
|
| ︙ | |||
63 64 65 66 67 68 69 | 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | - - + + + + + + + + + | } /// Problem update information. /// /// The solver calls the `update` method of the problem regularly. /// This method can modify the problem by adding (or removing) /// variables. The possible updates are encoded in this type. |
| ︙ | |||
173 174 175 176 177 178 179 | 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | + + - + + - - + + - + + + + + + |
let mut primals = primals;
primals.pop().unwrap().1
}
/// Return updates of the problem.
///
/// The default implementation returns no updates.
fn update(
&mut self,
|
Changes to src/hkweighter.rs.
| ︙ | |||
18 19 20 21 22 23 24 | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | - - + + + + | //! //! The procedure is described in //! //! > Helmberg, C. and Kiwiel, K.C. (2002): A spectral bundle method //! > with bounds, Math. Programming A 93, 173--194 //! |
| ︙ | |||
79 80 81 82 83 84 85 | 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | + - + + - + |
if state.step == Step::Term {
self.eps_weight = 1e30;
self.iter = 0;
return if state.cur_y.len() == 0 || state.sgnorm < state.cur_y.len() as Real * 1e-10 {
1.0
} else {
state.sgnorm.max(1e-4)
}
|
| ︙ | |||
120 121 122 123 124 125 126 | 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | + - + |
self.model_max = self.model_max.max(state.nxt_mod);
let new_weight = if self.iter > 0 && cur_nxt > self.m_r * cur_mod {
w
} else if self.iter > 3 || state.nxt_val < self.model_max {
state.weight / 2.0
} else {
state.weight
}
|
| ︙ |
Changes to src/lib.rs.
| ︙ | |||
12 13 14 15 16 17 18 | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | - - - - - - - - - - - + - + - + - + - + | // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/> // //! Proximal bundle method implementation. |
Changes to src/master/base.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | - + - + |
|
| ︙ | |||
42 43 44 45 46 47 48 | 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | - + + + + |
Solver(err) => write!(fmt, "Solver error: {}", err),
Custom(err) => err.fmt(fmt),
}
}
}
impl Error for MasterProblemError {
|
| ︙ | |||
79 80 81 82 83 84 85 | 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | - + - + |
/// Set the maximal number of inner iterations.
fn set_max_updates(&mut self, max_updates: usize) -> Result<()>;
/// Return the current number of inner iterations.
fn cnt_updates(&self) -> usize;
|
| ︙ |
Changes to src/master/boxed.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | - + - - - + + + + - + - |
|
| ︙ | |||
157 158 159 160 161 162 163 | 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | + - + - + - |
0.0
}
} else if ub < INFINITY {
ub * eta
} else {
0.0
}
})
|
| ︙ | |||
206 207 208 209 210 211 212 | 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | - + - + |
fn set_weight(&mut self, weight: Real) -> Result<()> {
self.master.set_weight(weight)
}
fn add_vars(
&mut self,
bounds: &[(Option<usize>, Real, Real)],
|
| ︙ |
Changes to src/master/cpx.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | - + - - + + + + + + + - - + - |
|
| ︙ | |||
153 154 155 156 157 158 159 | 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | - + - - + + |
}
}
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
|
| ︙ | |||
203 204 205 206 207 208 209 | 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | - + |
// self.force_update = true;
Ok(())
}
fn solve(&mut self, eta: &DVector, _fbound: Real, _augbound: Real, _relprec: Real) -> Result<()> {
if self.force_update || !self.updateinds.is_empty() {
|
| ︙ | |||
230 231 232 233 234 235 236 237 238 239 240 241 242 243 | 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | + + + + |
nvars as c_int,
inds.as_ptr(),
c.as_ptr()
));
}
trycpx!(cpx::qpopt(cpx::env(), self.lp));
let solstat = unsafe { cpx::getstat(cpx::env(), self.lp) };
if solstat != cpx::Stat::Optimal.to_c() && solstat != cpx::Stat::NumBest.to_c() {
warn!("Problem solving the master QP with Cplex (status: {:?})", solstat)
}
let mut sol = vec![0.0; nvars];
trycpx!(cpx::getx(cpx::env(), self.lp, sol.as_mut_ptr(), 0, nvars as c_int - 1));
let mut idx = 0;
let mut mults = Vec::with_capacity(nvars);
let mut mins = Vec::with_capacity(nvars);
for fidx in 0..self.minorants.len() {
|
| ︙ | |||
292 293 294 295 296 297 298 | 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | + - + |
sum_coeffs += self.opt_mults[fidx][self.index2min[i].1];
}
let aggr_coeffs = if sum_coeffs != 0.0 {
mins.iter()
.map(|&i| self.opt_mults[fidx][self.index2min[i].1] / sum_coeffs)
.collect::<DVector>()
} else {
// All coefficients are zero, we can simply remove all but one of the minorants.
|
| ︙ |
Changes to src/master/minimal.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | - + - - + + + + + |
|
| ︙ | |||
121 122 123 124 125 126 127 | 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | - + |
Ok(self.minorants.len() - 1)
}
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
|
| ︙ |
Changes to src/master/mod.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | - + - |
|
| ︙ | |||
34 35 36 37 38 39 40 | 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | - + - + |
//! * changing the weight parameter $w$,
//! * modifying $\hat{f}$ by adding or removing linear functions $\ell_i$,
//! * moving the center of the linear functions $\ell_i$ (and the
//! bounds), i.e. replacing $\hat{f}$ by $d \mapsto \hat{f}(d -
//! \hat{d})$ for some given $\hat{d} \in \mathbb{R}\^n$.
mod base;
|
Changes to src/master/unconstrained.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | - + - + - + - - - |
|
| ︙ | |||
77 78 79 80 81 82 83 | 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | - + |
/// The variables in `changed` have been changed, so the subgradient
/// information must be updated. Furthermore, `nnew` new variables
/// are added.
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
|
| ︙ |
Changes to src/mcf/mod.rs.
| ︙ | |||
13 14 15 16 17 18 19 | 13 14 15 16 17 18 19 20 21 22 23 | - + - + | // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/> // //! Multi-commodity min-cost-flow subproblems. mod solver; |
Changes to src/mcf/problem.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | - + - - + + + + |
|
| ︙ | |||
35 36 37 38 39 40 41 | 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | - + |
write!(fmt, "Format error: {}", self.msg)
}
}
impl Error for MMCFFormatError {}
/// Result type of the MMCFProblem.
|
| ︙ | |||
65 66 67 68 69 70 71 | 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | - - + + + - + - + - + - + - - - - + + + + - + - + - - - - - - - + + + + + + + - + - + - + - - + + |
c: Vec<DVector>,
}
impl MMCFProblem {
pub fn read_mnetgen(basename: &str) -> Result<MMCFProblem> {
let mut buffer = String::new();
{
|
| ︙ | |||
204 205 206 207 208 209 210 | 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | + - + |
let mut aggr = primals[0]
.1
.iter()
.map(|x| {
let mut r = dvec![];
r.scal(primals[0].0, x);
r
})
|
| ︙ | |||
264 265 266 267 268 269 270 | 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | - + - - + + - + - + - + - - + + |
}
}
}
debug!("y={:?}", y);
for i in 0..self.nets.len() {
debug!("c[{}]={}", i, self.c[i]);
|
| ︙ |
Changes to src/mcf/solver.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | - + - + + + |
|
| ︙ | |||
44 45 46 47 48 49 50 | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | - + |
impl Solver {
pub fn new(nnodes: usize) -> Result<Solver> {
let mut status: c_int;
let mut net = ptr::null_mut();
unsafe {
|
| ︙ | |||
73 74 75 76 77 78 79 | 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | + - + |
if status != 0 {
let msg = CString::new(vec![0; cpx::MESSAGE_BUF_SIZE]).unwrap().into_raw();
cpx::geterrorstring(cpx::env(), status, msg);
cpx::NETfreeprob(cpx::env(), &mut net);
return Err(cpx::CplexError {
code: status,
msg: CString::from_raw(msg).to_string_lossy().into_owned(),
}
|
| ︙ |
Changes to src/minorant.rs.
| ︙ | |||
12 13 14 15 16 17 18 | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | - + | // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/> // //! A linear minorant. |
| ︙ | |||
38 39 40 41 42 43 44 | 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | - + |
/// The linear term.
pub linear: DVector,
}
impl fmt::Display for Minorant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| ︙ |
Changes to src/solver.rs.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | - + - - + + - - + + + + |
|
| ︙ | |||
46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | + + |
Parameter(ParameterError),
/// The lower bound of a variable is larger than the upper bound.
InvalidBounds { lower: Real, upper: Real },
/// The value of a variable is outside its bounds.
ViolatedBounds { lower: Real, upper: Real, value: Real },
/// The variable index is out of bounds.
InvalidVariable { index: usize, nvars: usize },
/// A subproblem index is out of bounds.
InvalidSubproblem { subproblem: usize, nsubs: usize },
/// Iteration limit has been reached.
IterationLimit { limit: usize },
}
impl<E: fmt::Display> fmt::Display for SolverError<E> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use self::SolverError::*;
|
| ︙ | |||
69 70 71 72 73 74 75 76 77 78 79 80 81 | 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | + + + + + - + + + + + + + |
fmt,
"Violated bounds, lower:{}, upper:{}, value:{}",
lower, upper, value
),
InvalidVariable { index, nvars } => {
write!(fmt, "Variable index out of bounds, got:{} must be < {}", index, nvars)
}
InvalidSubproblem { subproblem, nsubs } => write!(
fmt,
"Subproblem index out of bounds, got:{} must be < {}",
subproblem, nsubs
),
IterationLimit { limit } => write!(fmt, "The iteration limit of {} has been reached.", limit),
}
}
}
impl<E: Error> Error for SolverError<E> {
|
| ︙ | |||
319 320 321 322 323 324 325 | 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | - + - - - + + + - + - + - + - + |
Descent,
/// No step but the algorithm has been terminated.
Term,
}
/// Information about a minorant.
#[derive(Debug, Clone)]
|
| ︙ | |||
451 452 453 454 455 456 457 | 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | - + - + + + + |
* Time when the solution process started.
*
* This is actually the time of the last call to `Solver::init`.
*/
start_time: Instant,
/// The master problem.
|
| ︙ | |||
499 500 501 502 503 504 505 | 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | - + - - + |
nxt_mods: dvec![],
new_cutval: 0.0,
sgnorm: 0.0,
expected_progress: 0.0,
cnt_descent: 0,
cnt_null: 0,
start_time: Instant::now(),
|
| ︙ | |||
572 573 574 575 576 577 578 | 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | - + + + + - - + + + + + + + + - + |
self.nxt_mods.init0(m);
self.start_time = Instant::now();
Ok(())
}
|
| ︙ | |||
616 617 618 619 620 621 622 623 624 625 626 627 628 629 | 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | + |
///
/// Calling this function typically triggers the problem to
/// separate new constraints depending on the current solution.
fn update_problem(&mut self, term: Step) -> Result<bool, SolverError<P::Err>> {
let updates = {
let state = UpdateState {
minorants: &self.minorants,
primals: &self.primals,
step: term,
iteration_info: &self.iterinfos,
// this is a dirty trick: when updating the center, we
// simply swapped the `cur_*` fields with the `nxt_*`
// fields
cur_y: if term == Step::Descent {
&self.nxt_y
|
| ︙ | |||
675 676 677 678 679 680 681 682 683 684 685 686 | 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 | + + + + + + + + + + + + + + + - - + + - - - - - - - - - + + + + + + + + |
}
let (lower, upper) = self.bounds[index];
if value < lower || value > upper {
return Err(SolverError::ViolatedBounds { lower, upper, value });
}
newvars.push((Some(index), lower - value, upper - value, value));
}
Update::ModifyPrimals { subproblem, modify } => {
if subproblem >= self.minorants.len() {
return Err(SolverError::InvalidSubproblem {
subproblem: subproblem,
nsubs: self.minorants.len(),
});
}
for m in &mut self.minorants[subproblem] {
if let Some(ref mut p) = self.primals[m.index] {
if let Err(err) = modify(p) {
return Err(SolverError::Update(err));
}
}
}
}
}
}
if !newvars.is_empty() {
let problem = &mut self.problem;
|
| ︙ | |||
716 717 718 719 720 721 722 | 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 | - + |
/// This function returns all currently used minorants $x_i$ along
/// with their coefficients $\alpha_i$. The aggregated primal can
/// be computed by combining the minorants $\bar{x} =
/// \sum_{i=1}\^m \alpha_i x_i$.
pub fn aggregated_primals(&self, subproblem: usize) -> Vec<(Real, &P::Primal)> {
self.minorants[subproblem]
.iter()
|
| ︙ | |||
764 765 766 767 768 769 770 | 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | - + - - - + - - - + - - + - - - + - + - + - + + + + - - + + - + - + |
* information.
*/
fn init_master(&mut self) -> Result<(), SolverError<P::Err>> {
let m = self.problem.num_subproblems();
self.master = if m == 1 && self.params.max_bundle_size == 2 {
debug!("Use minimal master problem");
|
| ︙ | |||
881 882 883 884 885 886 887 | 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | - - - + + + + + + - - - + + + - - + - + - + - + |
for i in 0..self.problem.num_subproblems() {
let n = self.master.num_minorants(i);
if n >= self.params.max_bundle_size {
// aggregate minorants with smallest coefficients
self.minorants[i].sort_by_key(|m| -((1e6 * m.multiplier) as isize));
let aggr = self.minorants[i].split_off(self.params.max_bundle_size - 2);
let aggr_sum = aggr.iter().map(|m| m.multiplier).sum();
|
| ︙ | |||
978 979 980 981 982 983 984 985 | 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 | + - + - - - - + + + + |
nxt_lb += fun_lb;
nxt_ub += fun_ub;
self.nxt_vals[fidx] = fun_ub;
// move center of minorant to cur_y
nxt_minorant.move_center(-1.0, &self.nxt_d);
self.new_cutval += nxt_minorant.constant;
let minidx = self.master.add_minorant(fidx, nxt_minorant)?;
self.minorants[fidx].push(MinorantInfo {
|
| ︙ |
Changes to src/vector.rs.
| ︙ | |||
12 13 14 15 16 17 18 19 20 | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | + - |
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
//! Finite-dimensional sparse and dense vectors.
use crate::Real;
use std::fmt;
use std::ops::{Deref, DerefMut};
|
| ︙ | |||
39 40 41 42 43 44 45 | 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | - + - + - + - + |
fn deref_mut(&mut self) -> &mut Vec<Real> {
&mut self.0
}
}
impl fmt::Display for DVector {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
| ︙ | |||
87 88 89 90 91 92 93 | 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | - + - + - + |
impl fmt::Display for Vector {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Vector::Dense(ref v) => write!(f, "{}", v),
Vector::Sparse { size, ref elems } => {
let mut it = elems.iter();
|
| ︙ | |||
187 188 189 190 191 192 193 | 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | - + |
/// the smaller vector are assumed to be 0.0.
pub fn add_scaled_begin(&mut self, alpha: Real, y: &DVector) {
for (x, y) in self.iter_mut().zip(y.iter()) {
*x += alpha * y;
}
let n = self.len();
if n < y.len() {
|
| ︙ | |||
281 282 283 284 285 286 287 | 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | + + + + + + + + |
unsafe { *v.get_unchecked_mut(i) = x };
}
DVector(v)
}
}
}
}
#[test]
fn test_add_scaled_begin() {
let mut x = dvec![1.0; 5];
let y = dvec![2.0; 7];
x.add_scaled_begin(3.0, &y);
assert_eq!(x, dvec![7.0, 7.0, 7.0, 7.0, 7.0, 6.0, 6.0]);
}
|