"Quantity" (比较方法)
Details
- The "Quantity" comparison method considers two quantities to be equivalent if they do not differ beyond a given tolerance.
- All specified quantities must have compatible units.
- This tolerance should be expressed as any Quantity consistent with the answer key and is set using the Tolerance option in AssessmentFunction.
- The default tolerance has a zero magnitude.
- By default, the comparison between the answer key and the answer is computed as the absolute difference. The DistanceFunction option can be used to specify a custom distance metric.
- When multiple values in the answer key are within the tolerance range of an answer, the closest value is selected.
范例
打开所有单元 关闭所有单元基本范例 (2)
Create an AssessmentFunction for a distance quantity question with a tolerance:
distaf = AssessmentFunction[Quantity[10, "Yards"], "Quantity", Tolerance -> Quantity[1, "Feet"]]Use it to assess answers specified in any compatible unit:
distaf[Quantity[10.33, "Yards"]]distaf[Quantity[10, "Yards"] + Quantity[11.999, "Inches"]]distaf[Quantity[9, "Meters"]]Assess the distance from the Earth to the Sun, allowing a 10% margin of error:
sundistcheck = AssessmentFunction[{Entity["Star", "Sun"][EntityProperty["Star", "DistanceFromEarth"]]}, Tolerance -> Quantity[0.1, "AstronomicalUnit"]]sundistcheck[Quantity[1.4*^8, "Kilometers"]]Provide an answer computed based on the time for light to reach Earth:
sundistcheck[Quantity[1, "SpeedOfLight"] * Quantity[8, "Minutes"]]Scope (1)
Generate building height data for use in an answer key:
buildingheights = EntityValue[EntityClass["Building", {EntityProperty["Building", "EntityClasses"] -> EntityClass["Building", "Skyscraper"], EntityProperty["Building", "Height"] -> TakeLargest[5]}], EntityProperty["Building", "Height"]]Create an assessment using the data with multiple scores and a tolerance:
testheights = AssessmentFunction[Thread[buildingheights -> Reverse@Range[5]], "Quantity", Tolerance -> Quantity[50, "Meters"]]Verify all the values and scores in the answer key:
Information[testheights, "Key"]Assess an answer and retrieve the score directly. The score corresponds to the closest answer key given the tolerance:
testheights[Quantity[610, "Meters"]]["Score"]Applications (1)
Retrieve hydrogen spectra wavelengths and combine values within an angstom:
hydrogenvisiblespectra = Union@Round[Select[EntityClass["AtomicLine", {"Hydrogen", 1}]["Wavelength"], Quantity[4000, "Angstroms"] < # < Quantity[7000, "Angstroms"]&], 1]Create a QuestionObject for reading a spectrum diagram:
QuestionObject[QuestionInterface["MultipleShortAnswers", <|"Prompt" -> Column[{"What are Hydrogen spectra wavelengths in the visible region ?", WolframAlpha["Hydrogen spectra", {{"Result", 2}, "Image"}]}], "Interpreter" -> Restricted["StructuredQuantity", "Angstrom"]|>], AssessmentFunction[ hydrogenvisiblespectra -> 1, <|"ListAssessment" -> "AllElementsOrderless"|>, Tolerance -> Quantity[50, "Angstroms"]]]Properties & Relations (2)
The "Quantity" comparison method will be automatically inferred if all the answer keys are Quantity expressions:
AssessmentFunction[{Quantity[10, "Meters"], Quantity[2, "Miles"]}]Otherwise the fallback "Expression" comparison method will be inferred:
AssessmentFunction[{Quantity[10, "Meters"], 2}]The "Quantity" comparison always automates unit conversion:
AssessmentFunction[{Quantity[1, "Days"]}, "Quantity"][Quantity[24, "Hours"]]To prevent unit conversion and require that answers exactly match the answer key, use a different method like "Number" or "Expression":
AssessmentFunction[{Quantity[1, "Days"]}, "Expression"][Quantity[24, "Hours"]]AssessmentFunction[{Quantity[1, "Days"]}, "Expression"][Quantity[1, "Days"]]AssessmentFunction[{1}, "Number"][24]Possible Issues (1)
When an assessment has zero tolerance, the comparison is performed at machine precision:
zerotolerance = AssessmentFunction[Quantity[2, "Days"], "Quantity"]Equivalent quantities are considered correct:
zerotolerance[Quantity[2 * 24 * 60, "Minutes"]]Small deviations are considered incorrect unless they are are less than machine precision:
zerotolerance[Quantity[2 * 24 * 60, "Minutes"] + Quantity[1, "Nanoseconds"]]zerotolerance[Quantity[2 * 24 * 60, "Minutes"] + Quantity[1, "Picoseconds"]]