1 people like it.

Generate simple linear equation systems

Generates simple systems of linear equations, suitable for being solved by someone who's just started learning about them. Change the ranges to make them suitable for solving without a calculator.

 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: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
type Variable = char * int // symbol * value
type Term = int * Variable list // coefficient * variables
type Equation = Term list * int * int // terms * constant-term * result

let genEquationSet numVars =
    let rng = new System.Random()
    let vars : Variable[] =
        "acdefghijkmnpqrtuvwxy".ToCharArray() // avoid letters that could be confused with numbers
        |> Array.sortBy(fun _ -> rng.Next())
        |> Seq.take numVars
        |> Seq.map (fun ch -> ch, rng.Next(-25, 26)) // range of values that variables may take
        |> Seq.toArray
    let rec triangular freeVars accum =
        match freeVars with
        | 0 -> accum
        | _ ->
            let constTerm =
                if rng.NextDouble() > 0.3 then rng.Next(-99,100) // constant term range of values
                else 0
            let simpleTerms, simpleResult =
                let coeffs =
                    Seq.initInfinite (fun _ -> rng.Next(-15,16)) // range of values for term coefficients
                    |> Seq.filter ((<>) 0)
                    |> Seq.take freeVars
                    |> Seq.toArray
                let terms : Term list =
                    vars
                    |> Seq.take freeVars
                    |> Seq.mapi (fun i (ch,value) -> coeffs.[i], [ch,value])
                    |> Seq.toList
                let result =
                    terms |> Seq.fold (fun state (cf,[_,v]) -> state + cf*v) 0
                terms, result
            let e : Equation = simpleTerms, constTerm, simpleResult+constTerm
            triangular (freeVars-1) (e::accum)
            |> List.rev
    let stringifyEqn (e : Equation) =
        let terms, constterm, result = e
        let sb = System.Text.StringBuilder()
        for (cf,vars) in terms do
            if cf >= 0 then sb.Append '+' |> ignore
            sb.Append cf |> ignore
            for (sym,_) in vars do
                sb.Append sym |> ignore
        if constterm > 0 then sb.AppendFormat("+{0}",constterm) |> ignore
        elif constterm < 0 then sb.Append constterm |> ignore
        sb.AppendFormat("={0}",result) |> ignore
        sb.ToString().TrimStart('+')
    triangular vars.Length []
    |> List.map stringifyEqn

// usage:
// > genEquationSet 3;;
// val it : string list = ["14u-12i+2q+59=149"; "11u-1i=124"; "10u=120"]
// > genEquationSet 2;;
// val it : string list = ["-3j-82=-97"; "-12j+7y-74=-295"]
// > genEquationSet 4;;
// val it : string list = ["10h+81=161"; "15h-15w+7=382"; "10h-14w+6g-28=356"; "-6h+9w+4g-13f+97=18"]
Multiple items
val char : value:'T -> char (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.char

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type Term = int * Variable list

Full name: Script.Term
type Variable = char * int

Full name: Script.Variable
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
type Equation = Term list * int * int

Full name: Script.Equation
val genEquationSet : numVars:int -> string list

Full name: Script.genEquationSet
val numVars : int
val rng : System.Random
namespace System
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
System.Random() : unit
System.Random(Seed: int) : unit
val vars : Variable []
module Array

from Microsoft.FSharp.Collections
val sortBy : projection:('T -> 'Key) -> array:'T [] -> 'T [] (requires comparison)

Full name: Microsoft.FSharp.Collections.Array.sortBy
System.Random.Next() : int
System.Random.Next(maxValue: int) : int
System.Random.Next(minValue: int, maxValue: int) : int
module Seq

from Microsoft.FSharp.Collections
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val ch : char
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
val triangular : (int -> Equation list -> Equation list)
val freeVars : int
val accum : Equation list
val constTerm : int
System.Random.NextDouble() : float
val simpleTerms : Term list
val simpleResult : int
val coeffs : int []
val initInfinite : initializer:(int -> 'T) -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.initInfinite
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val terms : Term list
val mapi : mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.mapi
val i : int
val value : int
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
val result : int
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val state : int
val cf : int
val v : int
val e : Equation
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
val stringifyEqn : (Equation -> string)
val constterm : int
val sb : System.Text.StringBuilder
namespace System.Text
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 18 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 4 overloads
  member AppendLine : unit -> StringBuilder + 1 overload
  member Capacity : int with get, set
  member Chars : int -> char with get, set
  member Clear : unit -> StringBuilder
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EnsureCapacity : capacity:int -> int
  member Equals : sb:StringBuilder -> bool
  ...

Full name: System.Text.StringBuilder

--------------------
System.Text.StringBuilder() : unit
System.Text.StringBuilder(capacity: int) : unit
System.Text.StringBuilder(value: string) : unit
System.Text.StringBuilder(value: string, capacity: int) : unit
System.Text.StringBuilder(capacity: int, maxCapacity: int) : unit
System.Text.StringBuilder(value: string, startIndex: int, length: int, capacity: int) : unit
val vars : Variable list
System.Text.StringBuilder.Append(value: char []) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: obj) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: uint64) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: uint32) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: uint16) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: decimal) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: float) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: float32) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: int64) : System.Text.StringBuilder
   (+0 other overloads)
System.Text.StringBuilder.Append(value: int) : System.Text.StringBuilder
   (+0 other overloads)
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val sym : char
System.Text.StringBuilder.AppendFormat(format: string, [<System.ParamArray>] args: obj []) : System.Text.StringBuilder
System.Text.StringBuilder.AppendFormat(format: string, arg0: obj) : System.Text.StringBuilder
System.Text.StringBuilder.AppendFormat(provider: System.IFormatProvider, format: string, [<System.ParamArray>] args: obj []) : System.Text.StringBuilder
System.Text.StringBuilder.AppendFormat(format: string, arg0: obj, arg1: obj) : System.Text.StringBuilder
System.Text.StringBuilder.AppendFormat(format: string, arg0: obj, arg1: obj, arg2: obj) : System.Text.StringBuilder
System.Text.StringBuilder.ToString() : string
System.Text.StringBuilder.ToString(startIndex: int, length: int) : string
property System.Array.Length: int
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
Raw view Test code New version

More information

Link:http://fssnip.net/lM
Posted:10 years ago
Author:Anonymous
Tags: education , learning , mathematics